annotate core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@16 1 <?php
Chris@16 2
Chris@16 3 namespace Drupal\Tests\image\Functional;
Chris@16 4
Chris@18 5 use Drupal\Core\Url;
Chris@16 6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@16 7 use Drupal\field\Entity\FieldStorageConfig;
Chris@16 8 use Drupal\Tests\TestFileCreationTrait;
Chris@18 9 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
Chris@16 10 use Drupal\user\RoleInterface;
Chris@16 11 use Drupal\image\Entity\ImageStyle;
Chris@16 12
Chris@16 13 /**
Chris@16 14 * Tests the display of image fields.
Chris@16 15 *
Chris@16 16 * @group image
Chris@16 17 */
Chris@16 18 class ImageFieldDisplayTest extends ImageFieldTestBase {
Chris@16 19
Chris@16 20 use AssertPageCacheContextsAndTagsTrait;
Chris@16 21 use TestFileCreationTrait {
Chris@16 22 getTestFiles as drupalGetTestFiles;
Chris@16 23 compareFiles as drupalCompareFiles;
Chris@16 24 }
Chris@16 25
Chris@16 26 protected $dumpHeaders = TRUE;
Chris@16 27
Chris@16 28 /**
Chris@16 29 * Modules to enable.
Chris@16 30 *
Chris@16 31 * @var array
Chris@16 32 */
Chris@16 33 public static $modules = ['field_ui'];
Chris@16 34
Chris@16 35 /**
Chris@16 36 * Test image formatters on node display for public files.
Chris@16 37 */
Chris@16 38 public function testImageFieldFormattersPublic() {
Chris@16 39 $this->_testImageFieldFormatters('public');
Chris@16 40 }
Chris@16 41
Chris@16 42 /**
Chris@16 43 * Test image formatters on node display for private files.
Chris@16 44 */
Chris@16 45 public function testImageFieldFormattersPrivate() {
Chris@16 46 // Remove access content permission from anonymous users.
Chris@16 47 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, ['access content' => FALSE]);
Chris@16 48 $this->_testImageFieldFormatters('private');
Chris@16 49 }
Chris@16 50
Chris@16 51 /**
Chris@16 52 * Test image formatters on node display.
Chris@16 53 */
Chris@16 54 public function _testImageFieldFormatters($scheme) {
Chris@16 55 /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@16 56 $renderer = $this->container->get('renderer');
Chris@16 57 $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@16 58 $field_name = strtolower($this->randomMachineName());
Chris@16 59 $field_settings = ['alt_field_required' => 0];
Chris@16 60 $instance = $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme], $field_settings);
Chris@16 61
Chris@16 62 // Go to manage display page.
Chris@16 63 $this->drupalGet("admin/structure/types/manage/article/display");
Chris@16 64
Chris@16 65 // Test for existence of link to image styles configuration.
Chris@16 66 $this->drupalPostForm(NULL, [], "{$field_name}_settings_edit");
Chris@18 67 $this->assertLinkByHref(Url::fromRoute('entity.image_style.collection')->toString(), 0, 'Link to image styles configuration is found');
Chris@16 68
Chris@16 69 // Remove 'administer image styles' permission from testing admin user.
Chris@16 70 $admin_user_roles = $this->adminUser->getRoles(TRUE);
Chris@16 71 user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => FALSE]);
Chris@16 72
Chris@16 73 // Go to manage display page again.
Chris@16 74 $this->drupalGet("admin/structure/types/manage/article/display");
Chris@16 75
Chris@16 76 // Test for absence of link to image styles configuration.
Chris@16 77 $this->drupalPostForm(NULL, [], "{$field_name}_settings_edit");
Chris@18 78 $this->assertNoLinkByHref(Url::fromRoute('entity.image_style.collection')->toString(), 'Link to image styles configuration is absent when permissions are insufficient');
Chris@16 79
Chris@16 80 // Restore 'administer image styles' permission to testing admin user
Chris@16 81 user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => TRUE]);
Chris@16 82
Chris@16 83 // Create a new node with an image attached.
Chris@16 84 $test_image = current($this->drupalGetTestFiles('image'));
Chris@16 85
Chris@16 86 // Ensure that preview works.
Chris@16 87 $this->previewNodeImage($test_image, $field_name, 'article');
Chris@16 88
Chris@16 89 // After previewing, make the alt field required. It cannot be required
Chris@16 90 // during preview because the form validation will fail.
Chris@16 91 $instance->setSetting('alt_field_required', 1);
Chris@16 92 $instance->save();
Chris@16 93
Chris@16 94 // Create alt text for the image.
Chris@16 95 $alt = $this->randomMachineName();
Chris@16 96
Chris@16 97 // Save node.
Chris@16 98 $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
Chris@16 99 $node_storage->resetCache([$nid]);
Chris@16 100 $node = $node_storage->load($nid);
Chris@16 101
Chris@16 102 // Test that the default formatter is being used.
Chris@16 103 $file = $node->{$field_name}->entity;
Chris@16 104 $image_uri = $file->getFileUri();
Chris@16 105 $image = [
Chris@16 106 '#theme' => 'image',
Chris@16 107 '#uri' => $image_uri,
Chris@16 108 '#width' => 40,
Chris@16 109 '#height' => 20,
Chris@16 110 '#alt' => $alt,
Chris@16 111 ];
Chris@16 112 $default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
Chris@16 113 $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
Chris@16 114
Chris@16 115 // Test the image linked to file formatter.
Chris@16 116 $display_options = [
Chris@16 117 'type' => 'image',
Chris@16 118 'settings' => ['image_link' => 'file'],
Chris@16 119 ];
Chris@16 120 $display = entity_get_display('node', $node->getType(), 'default');
Chris@16 121 $display->setComponent($field_name, $display_options)
Chris@16 122 ->save();
Chris@16 123
Chris@16 124 $image = [
Chris@16 125 '#theme' => 'image',
Chris@16 126 '#uri' => $image_uri,
Chris@16 127 '#width' => 40,
Chris@16 128 '#height' => 20,
Chris@16 129 '#alt' => $alt,
Chris@16 130 ];
Chris@16 131 $default_output = '<a href="' . file_create_url($image_uri) . '">' . $renderer->renderRoot($image) . '</a>';
Chris@16 132 $this->drupalGet('node/' . $nid);
Chris@16 133 $this->assertCacheTag($file->getCacheTags()[0]);
Chris@16 134 // @todo Remove in https://www.drupal.org/node/2646744.
Chris@16 135 $this->assertCacheContext('url.site');
Chris@16 136 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
Chris@16 137 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
Chris@16 138 $this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
Chris@16 139 // Verify that the image can be downloaded.
Chris@16 140 $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
Chris@16 141 if ($scheme == 'private') {
Chris@16 142 // Only verify HTTP headers when using private scheme and the headers are
Chris@16 143 // sent by Drupal.
Chris@16 144 $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', 'Content-Type header was sent.');
Chris@16 145 $this->assertTrue(strstr($this->drupalGetHeader('Cache-Control'), 'private') !== FALSE, 'Cache-Control header was sent.');
Chris@16 146
Chris@16 147 // Log out and try to access the file.
Chris@16 148 $this->drupalLogout();
Chris@16 149 $this->drupalGet(file_create_url($image_uri));
Chris@16 150 $this->assertResponse('403', 'Access denied to original image as anonymous user.');
Chris@16 151
Chris@16 152 // Log in again.
Chris@16 153 $this->drupalLogin($this->adminUser);
Chris@16 154 }
Chris@16 155
Chris@16 156 // Test the image linked to content formatter.
Chris@16 157 $display_options['settings']['image_link'] = 'content';
Chris@16 158 $display->setComponent($field_name, $display_options)
Chris@16 159 ->save();
Chris@16 160 $image = [
Chris@16 161 '#theme' => 'image',
Chris@16 162 '#uri' => $image_uri,
Chris@16 163 '#width' => 40,
Chris@16 164 '#height' => 20,
Chris@16 165 ];
Chris@16 166 $this->drupalGet('node/' . $nid);
Chris@16 167 $this->assertCacheTag($file->getCacheTags()[0]);
Chris@16 168 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
Chris@16 169 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
Chris@16 170 $elements = $this->xpath(
Chris@16 171 '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]',
Chris@16 172 [
Chris@18 173 ':path' => $node->toUrl()->toString(),
Chris@16 174 ':url' => file_url_transform_relative(file_create_url($image['#uri'])),
Chris@16 175 ':width' => $image['#width'],
Chris@16 176 ':height' => $image['#height'],
Chris@16 177 ':alt' => $alt,
Chris@16 178 ]
Chris@16 179 );
Chris@16 180 $this->assertEqual(count($elements), 1, 'Image linked to content formatter displaying correctly on full node view.');
Chris@16 181
Chris@16 182 // Test the image style 'thumbnail' formatter.
Chris@16 183 $display_options['settings']['image_link'] = '';
Chris@16 184 $display_options['settings']['image_style'] = 'thumbnail';
Chris@16 185 $display->setComponent($field_name, $display_options)
Chris@16 186 ->save();
Chris@16 187
Chris@16 188 // Ensure the derivative image is generated so we do not have to deal with
Chris@16 189 // image style callback paths.
Chris@16 190 $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
Chris@16 191 $image_style = [
Chris@16 192 '#theme' => 'image_style',
Chris@16 193 '#uri' => $image_uri,
Chris@16 194 '#width' => 40,
Chris@16 195 '#height' => 20,
Chris@16 196 '#style_name' => 'thumbnail',
Chris@16 197 '#alt' => $alt,
Chris@16 198 ];
Chris@16 199 $default_output = $renderer->renderRoot($image_style);
Chris@16 200 $this->drupalGet('node/' . $nid);
Chris@16 201 $image_style = ImageStyle::load('thumbnail');
Chris@16 202 $this->assertCacheTag($image_style->getCacheTags()[0]);
Chris@16 203 $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
Chris@16 204
Chris@16 205 if ($scheme == 'private') {
Chris@16 206 // Log out and try to access the file.
Chris@16 207 $this->drupalLogout();
Chris@16 208 $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
Chris@16 209 $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
Chris@16 210 }
Chris@16 211
Chris@16 212 // Test the image URL formatter without an image style.
Chris@16 213 $display_options = [
Chris@16 214 'type' => 'image_url',
Chris@16 215 'settings' => ['image_style' => ''],
Chris@16 216 ];
Chris@16 217 $expected_url = file_url_transform_relative(file_create_url($image_uri));
Chris@16 218 $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
Chris@16 219
Chris@16 220 // Test the image URL formatter with an image style.
Chris@16 221 $display_options['settings']['image_style'] = 'thumbnail';
Chris@16 222 $expected_url = file_url_transform_relative(ImageStyle::load('thumbnail')->buildUrl($image_uri));
Chris@16 223 $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
Chris@16 224 }
Chris@16 225
Chris@16 226 /**
Chris@16 227 * Tests for image field settings.
Chris@16 228 */
Chris@16 229 public function testImageFieldSettings() {
Chris@16 230 /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@16 231 $renderer = $this->container->get('renderer');
Chris@16 232 $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@16 233 $test_image = current($this->drupalGetTestFiles('image'));
Chris@16 234 list(, $test_image_extension) = explode('.', $test_image->filename);
Chris@16 235 $field_name = strtolower($this->randomMachineName());
Chris@16 236 $field_settings = [
Chris@16 237 'alt_field' => 1,
Chris@16 238 'file_extensions' => $test_image_extension,
Chris@16 239 'max_filesize' => '50 KB',
Chris@16 240 'max_resolution' => '100x100',
Chris@16 241 'min_resolution' => '10x10',
Chris@16 242 'title_field' => 1,
Chris@16 243 ];
Chris@16 244 $widget_settings = [
Chris@16 245 'preview_image_style' => 'medium',
Chris@16 246 ];
Chris@16 247 $field = $this->createImageField($field_name, 'article', [], $field_settings, $widget_settings);
Chris@16 248
Chris@16 249 // Verify that the min/max resolution set on the field are properly
Chris@16 250 // extracted, and displayed, on the image field's configuration form.
Chris@16 251 $this->drupalGet('admin/structure/types/manage/article/fields/' . $field->id());
Chris@16 252 $this->assertFieldByName('settings[max_resolution][x]', '100', 'Expected max resolution X value of 100.');
Chris@16 253 $this->assertFieldByName('settings[max_resolution][y]', '100', 'Expected max resolution Y value of 100.');
Chris@16 254 $this->assertFieldByName('settings[min_resolution][x]', '10', 'Expected min resolution X value of 10.');
Chris@16 255 $this->assertFieldByName('settings[min_resolution][y]', '10', 'Expected min resolution Y value of 10.');
Chris@16 256
Chris@16 257 $this->drupalGet('node/add/article');
Chris@16 258 $this->assertText(t('50 KB limit.'), 'Image widget max file size is displayed on article form.');
Chris@16 259 $this->assertText(t('Allowed types: @extensions.', ['@extensions' => $test_image_extension]), 'Image widget allowed file types displayed on article form.');
Chris@16 260 $this->assertText(t('Images must be larger than 10x10 pixels. Images larger than 100x100 pixels will be resized.'), 'Image widget allowed resolution displayed on article form.');
Chris@16 261
Chris@16 262 // We have to create the article first and then edit it because the alt
Chris@16 263 // and title fields do not display until the image has been attached.
Chris@16 264
Chris@16 265 // Create alt text for the image.
Chris@16 266 $alt = $this->randomMachineName();
Chris@16 267
Chris@16 268 $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
Chris@16 269 $this->drupalGet('node/' . $nid . '/edit');
Chris@16 270
Chris@16 271 // Verify that the optional fields alt & title are saved & filled.
Chris@16 272 $this->assertFieldByName($field_name . '[0][alt]', $alt, 'Alt field displayed on article form.');
Chris@16 273 $this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.');
Chris@16 274
Chris@16 275 // Verify that the attached image is being previewed using the 'medium'
Chris@16 276 // style.
Chris@16 277 $node_storage->resetCache([$nid]);
Chris@16 278 $node = $node_storage->load($nid);
Chris@16 279 $file = $node->{$field_name}->entity;
Chris@16 280
Chris@18 281 $url = file_url_transform_relative(ImageStyle::load('medium')->buildUrl($file->getFileUri()));
Chris@16 282 $this->assertTrue($this->cssSelect('img[width=40][height=20][class=image-style-medium][src="' . $url . '"]'));
Chris@16 283
Chris@16 284 // Add alt/title fields to the image and verify that they are displayed.
Chris@16 285 $image = [
Chris@16 286 '#theme' => 'image',
Chris@16 287 '#uri' => $file->getFileUri(),
Chris@16 288 '#alt' => $alt,
Chris@16 289 '#title' => $this->randomMachineName(),
Chris@16 290 '#width' => 40,
Chris@16 291 '#height' => 20,
Chris@16 292 ];
Chris@16 293 $edit = [
Chris@16 294 $field_name . '[0][alt]' => $image['#alt'],
Chris@16 295 $field_name . '[0][title]' => $image['#title'],
Chris@16 296 ];
Chris@16 297 $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
Chris@16 298 $default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
Chris@16 299 $this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');
Chris@16 300
Chris@16 301 // Verify that alt/title longer than allowed results in a validation error.
Chris@16 302 $test_size = 2000;
Chris@16 303 $edit = [
Chris@16 304 $field_name . '[0][alt]' => $this->randomMachineName($test_size),
Chris@16 305 $field_name . '[0][title]' => $this->randomMachineName($test_size),
Chris@16 306 ];
Chris@16 307 $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
Chris@16 308 $schema = $field->getFieldStorageDefinition()->getSchema();
Chris@16 309 $this->assertRaw(t('Alternative text cannot be longer than %max characters but is currently %length characters long.', [
Chris@16 310 '%max' => $schema['columns']['alt']['length'],
Chris@16 311 '%length' => $test_size,
Chris@16 312 ]));
Chris@16 313 $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', [
Chris@16 314 '%max' => $schema['columns']['title']['length'],
Chris@16 315 '%length' => $test_size,
Chris@16 316 ]));
Chris@16 317
Chris@16 318 // Set cardinality to unlimited and add upload a second image.
Chris@16 319 // The image widget is extending on the file widget, but the image field
Chris@16 320 // type does not have the 'display_field' setting which is expected by
Chris@16 321 // the file widget. This resulted in notices before when cardinality is not
Chris@16 322 // 1, so we need to make sure the file widget prevents these notices by
Chris@16 323 // providing all settings, even if they are not used.
Chris@16 324 // @see FileWidget::formMultipleElements().
Chris@16 325 $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage', ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], t('Save field settings'));
Chris@16 326 $edit = [
Chris@16 327 'files[' . $field_name . '_1][]' => \Drupal::service('file_system')->realpath($test_image->uri),
Chris@16 328 ];
Chris@16 329 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
Chris@16 330 // Add the required alt text.
Chris@16 331 $this->drupalPostForm(NULL, [$field_name . '[1][alt]' => $alt], t('Save'));
Chris@16 332 $this->assertText(format_string('Article @title has been updated.', ['@title' => $node->getTitle()]));
Chris@16 333
Chris@16 334 // Assert ImageWidget::process() calls FieldWidget::process().
Chris@16 335 $this->drupalGet('node/' . $node->id() . '/edit');
Chris@16 336 $edit = [
Chris@16 337 'files[' . $field_name . '_2][]' => \Drupal::service('file_system')->realpath($test_image->uri),
Chris@16 338 ];
Chris@16 339 $this->drupalPostForm(NULL, $edit, $field_name . '_2_upload_button');
Chris@16 340 $this->assertSession()->elementNotExists('css', 'input[name="files[' . $field_name . '_2][]"]');
Chris@16 341 $this->assertSession()->elementExists('css', 'input[name="files[' . $field_name . '_3][]"]');
Chris@16 342 }
Chris@16 343
Chris@16 344 /**
Chris@16 345 * Test use of a default image with an image field.
Chris@16 346 */
Chris@16 347 public function testImageFieldDefaultImage() {
Chris@16 348 /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@16 349 $renderer = $this->container->get('renderer');
Chris@16 350
Chris@16 351 $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@16 352 // Create a new image field.
Chris@16 353 $field_name = strtolower($this->randomMachineName());
Chris@16 354 $this->createImageField($field_name, 'article');
Chris@16 355
Chris@16 356 // Create a new node, with no images and verify that no images are
Chris@16 357 // displayed.
Chris@16 358 $node = $this->drupalCreateNode(['type' => 'article']);
Chris@16 359 $this->drupalGet('node/' . $node->id());
Chris@16 360 // Verify that no image is displayed on the page by checking for the class
Chris@16 361 // that would be used on the image field.
Chris@18 362 $this->assertSession()->responseNotMatches('<div class="(.*?)field--name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');
Chris@16 363 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
Chris@16 364 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
Chris@16 365
Chris@16 366 // Add a default image to the public image field.
Chris@16 367 $images = $this->drupalGetTestFiles('image');
Chris@16 368 $alt = $this->randomString(512);
Chris@16 369 $title = $this->randomString(1024);
Chris@16 370 $edit = [
Chris@16 371 // Get the path of the 'image-test.png' file.
Chris@16 372 'files[settings_default_image_uuid]' => \Drupal::service('file_system')->realpath($images[0]->uri),
Chris@16 373 'settings[default_image][alt]' => $alt,
Chris@16 374 'settings[default_image][title]' => $title,
Chris@16 375 ];
Chris@16 376 $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings'));
Chris@16 377 // Clear field definition cache so the new default image is detected.
Chris@16 378 \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@16 379 $field_storage = FieldStorageConfig::loadByName('node', $field_name);
Chris@16 380 $default_image = $field_storage->getSetting('default_image');
Chris@18 381 $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
Chris@16 382 $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
Chris@16 383 $image = [
Chris@16 384 '#theme' => 'image',
Chris@16 385 '#uri' => $file->getFileUri(),
Chris@16 386 '#alt' => $alt,
Chris@16 387 '#title' => $title,
Chris@16 388 '#width' => 40,
Chris@16 389 '#height' => 20,
Chris@16 390 ];
Chris@16 391 $default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
Chris@16 392 $this->drupalGet('node/' . $node->id());
Chris@16 393 $this->assertCacheTag($file->getCacheTags()[0]);
Chris@16 394 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
Chris@16 395 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
Chris@16 396 $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.');
Chris@16 397
Chris@16 398 // Create a node with an image attached and ensure that the default image
Chris@16 399 // is not displayed.
Chris@16 400
Chris@16 401 // Create alt text for the image.
Chris@16 402 $alt = $this->randomMachineName();
Chris@16 403
Chris@16 404 // Upload the 'image-test.gif' file.
Chris@16 405 $nid = $this->uploadNodeImage($images[2], $field_name, 'article', $alt);
Chris@16 406 $node_storage->resetCache([$nid]);
Chris@16 407 $node = $node_storage->load($nid);
Chris@16 408 $file = $node->{$field_name}->entity;
Chris@16 409 $image = [
Chris@16 410 '#theme' => 'image',
Chris@16 411 '#uri' => $file->getFileUri(),
Chris@16 412 '#width' => 40,
Chris@16 413 '#height' => 20,
Chris@16 414 '#alt' => $alt,
Chris@16 415 ];
Chris@16 416 $image_output = str_replace("\n", NULL, $renderer->renderRoot($image));
Chris@16 417 $this->drupalGet('node/' . $nid);
Chris@16 418 $this->assertCacheTag($file->getCacheTags()[0]);
Chris@16 419 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
Chris@16 420 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
Chris@16 421 $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.');
Chris@16 422 $this->assertRaw($image_output, 'User supplied image is displayed.');
Chris@16 423
Chris@16 424 // Remove default image from the field and make sure it is no longer used.
Chris@16 425 // Can't use fillField cause Mink can't fill hidden fields.
Chris@16 426 $this->drupalGet("admin/structure/types/manage/article/fields/node.article.$field_name/storage");
Chris@16 427 $this->getSession()->getPage()->find('css', 'input[name="settings[default_image][uuid][fids]"]')->setValue(0);
Chris@16 428 $this->getSession()->getPage()->pressButton(t('Save field settings'));
Chris@16 429
Chris@16 430 // Clear field definition cache so the new default image is detected.
Chris@16 431 \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@16 432 $field_storage = FieldStorageConfig::loadByName('node', $field_name);
Chris@16 433 $default_image = $field_storage->getSetting('default_image');
Chris@16 434 $this->assertFalse($default_image['uuid'], 'Default image removed from field.');
Chris@16 435 // Create an image field that uses the private:// scheme and test that the
Chris@16 436 // default image works as expected.
Chris@16 437 $private_field_name = strtolower($this->randomMachineName());
Chris@16 438 $this->createImageField($private_field_name, 'article', ['uri_scheme' => 'private']);
Chris@16 439 // Add a default image to the new field.
Chris@16 440 $edit = [
Chris@16 441 // Get the path of the 'image-test.gif' file.
Chris@16 442 'files[settings_default_image_uuid]' => \Drupal::service('file_system')->realpath($images[2]->uri),
Chris@16 443 'settings[default_image][alt]' => $alt,
Chris@16 444 'settings[default_image][title]' => $title,
Chris@16 445 ];
Chris@16 446 $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/storage', $edit, t('Save field settings'));
Chris@16 447 // Clear field definition cache so the new default image is detected.
Chris@16 448 \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@16 449
Chris@16 450 $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name);
Chris@16 451 $default_image = $private_field_storage->getSetting('default_image');
Chris@18 452 $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
Chris@16 453 $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.');
Chris@16 454 $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
Chris@16 455 // Create a new node with no image attached and ensure that default private
Chris@16 456 // image is displayed.
Chris@16 457 $node = $this->drupalCreateNode(['type' => 'article']);
Chris@16 458 $image = [
Chris@16 459 '#theme' => 'image',
Chris@16 460 '#uri' => $file->getFileUri(),
Chris@16 461 '#alt' => $alt,
Chris@16 462 '#title' => $title,
Chris@16 463 '#width' => 40,
Chris@16 464 '#height' => 20,
Chris@16 465 ];
Chris@16 466 $default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
Chris@16 467 $this->drupalGet('node/' . $node->id());
Chris@16 468 $this->assertCacheTag($file->getCacheTags()[0]);
Chris@16 469 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
Chris@16 470 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
Chris@16 471 $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.');
Chris@16 472 }
Chris@16 473
Chris@16 474 }