annotate core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php @ 16:c2387f117808

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