annotate core/modules/image/src/Tests/ImageFieldDisplayTest.php @ 0:4c8ae668cc8c

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