comparison core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents c2387f117808
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\image\Functional; 3 namespace Drupal\Tests\image\Functional;
4 4
5 use Drupal\Core\Url;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface; 6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldStorageConfig; 7 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\Tests\TestFileCreationTrait; 8 use Drupal\Tests\TestFileCreationTrait;
8 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; 9 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
9 use Drupal\user\RoleInterface; 10 use Drupal\user\RoleInterface;
10 use Drupal\image\Entity\ImageStyle; 11 use Drupal\image\Entity\ImageStyle;
11 12
12 /** 13 /**
13 * Tests the display of image fields. 14 * Tests the display of image fields.
61 // Go to manage display page. 62 // Go to manage display page.
62 $this->drupalGet("admin/structure/types/manage/article/display"); 63 $this->drupalGet("admin/structure/types/manage/article/display");
63 64
64 // Test for existence of link to image styles configuration. 65 // Test for existence of link to image styles configuration.
65 $this->drupalPostForm(NULL, [], "{$field_name}_settings_edit"); 66 $this->drupalPostForm(NULL, [], "{$field_name}_settings_edit");
66 $this->assertLinkByHref(\Drupal::url('entity.image_style.collection'), 0, 'Link to image styles configuration is found'); 67 $this->assertLinkByHref(Url::fromRoute('entity.image_style.collection')->toString(), 0, 'Link to image styles configuration is found');
67 68
68 // Remove 'administer image styles' permission from testing admin user. 69 // Remove 'administer image styles' permission from testing admin user.
69 $admin_user_roles = $this->adminUser->getRoles(TRUE); 70 $admin_user_roles = $this->adminUser->getRoles(TRUE);
70 user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => FALSE]); 71 user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => FALSE]);
71 72
72 // Go to manage display page again. 73 // Go to manage display page again.
73 $this->drupalGet("admin/structure/types/manage/article/display"); 74 $this->drupalGet("admin/structure/types/manage/article/display");
74 75
75 // Test for absence of link to image styles configuration. 76 // Test for absence of link to image styles configuration.
76 $this->drupalPostForm(NULL, [], "{$field_name}_settings_edit"); 77 $this->drupalPostForm(NULL, [], "{$field_name}_settings_edit");
77 $this->assertNoLinkByHref(\Drupal::url('entity.image_style.collection'), 'Link to image styles configuration is absent when permissions are insufficient'); 78 $this->assertNoLinkByHref(Url::fromRoute('entity.image_style.collection')->toString(), 'Link to image styles configuration is absent when permissions are insufficient');
78 79
79 // Restore 'administer image styles' permission to testing admin user 80 // Restore 'administer image styles' permission to testing admin user
80 user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => TRUE]); 81 user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => TRUE]);
81 82
82 // Create a new node with an image attached. 83 // Create a new node with an image attached.
167 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); 168 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
168 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); 169 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
169 $elements = $this->xpath( 170 $elements = $this->xpath(
170 '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]', 171 '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]',
171 [ 172 [
172 ':path' => $node->url(), 173 ':path' => $node->toUrl()->toString(),
173 ':url' => file_url_transform_relative(file_create_url($image['#uri'])), 174 ':url' => file_url_transform_relative(file_create_url($image['#uri'])),
174 ':width' => $image['#width'], 175 ':width' => $image['#width'],
175 ':height' => $image['#height'], 176 ':height' => $image['#height'],
176 ':alt' => $alt, 177 ':alt' => $alt,
177 ] 178 ]
275 // style. 276 // style.
276 $node_storage->resetCache([$nid]); 277 $node_storage->resetCache([$nid]);
277 $node = $node_storage->load($nid); 278 $node = $node_storage->load($nid);
278 $file = $node->{$field_name}->entity; 279 $file = $node->{$field_name}->entity;
279 280
280 $url = file_url_transform_relative(file_create_url(ImageStyle::load('medium')->buildUrl($file->getFileUri()))); 281 $url = file_url_transform_relative(ImageStyle::load('medium')->buildUrl($file->getFileUri()));
281 $this->assertTrue($this->cssSelect('img[width=40][height=20][class=image-style-medium][src="' . $url . '"]')); 282 $this->assertTrue($this->cssSelect('img[width=40][height=20][class=image-style-medium][src="' . $url . '"]'));
282 283
283 // Add alt/title fields to the image and verify that they are displayed. 284 // Add alt/title fields to the image and verify that they are displayed.
284 $image = [ 285 $image = [
285 '#theme' => 'image', 286 '#theme' => 'image',
356 // displayed. 357 // displayed.
357 $node = $this->drupalCreateNode(['type' => 'article']); 358 $node = $this->drupalCreateNode(['type' => 'article']);
358 $this->drupalGet('node/' . $node->id()); 359 $this->drupalGet('node/' . $node->id());
359 // Verify that no image is displayed on the page by checking for the class 360 // Verify that no image is displayed on the page by checking for the class
360 // that would be used on the image field. 361 // that would be used on the image field.
361 $this->assertNoPattern('<div class="(.*?)field--name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.'); 362 $this->assertSession()->responseNotMatches('<div class="(.*?)field--name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');
362 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); 363 $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
363 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); 364 $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
364 365
365 // Add a default image to the public image field. 366 // Add a default image to the public image field.
366 $images = $this->drupalGetTestFiles('image'); 367 $images = $this->drupalGetTestFiles('image');
375 $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings')); 376 $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings'));
376 // Clear field definition cache so the new default image is detected. 377 // Clear field definition cache so the new default image is detected.
377 \Drupal::entityManager()->clearCachedFieldDefinitions(); 378 \Drupal::entityManager()->clearCachedFieldDefinitions();
378 $field_storage = FieldStorageConfig::loadByName('node', $field_name); 379 $field_storage = FieldStorageConfig::loadByName('node', $field_name);
379 $default_image = $field_storage->getSetting('default_image'); 380 $default_image = $field_storage->getSetting('default_image');
380 $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']); 381 $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
381 $this->assertTrue($file->isPermanent(), 'The default image status is permanent.'); 382 $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
382 $image = [ 383 $image = [
383 '#theme' => 'image', 384 '#theme' => 'image',
384 '#uri' => $file->getFileUri(), 385 '#uri' => $file->getFileUri(),
385 '#alt' => $alt, 386 '#alt' => $alt,
446 // Clear field definition cache so the new default image is detected. 447 // Clear field definition cache so the new default image is detected.
447 \Drupal::entityManager()->clearCachedFieldDefinitions(); 448 \Drupal::entityManager()->clearCachedFieldDefinitions();
448 449
449 $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name); 450 $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name);
450 $default_image = $private_field_storage->getSetting('default_image'); 451 $default_image = $private_field_storage->getSetting('default_image');
451 $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']); 452 $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
452 $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.'); 453 $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.');
453 $this->assertTrue($file->isPermanent(), 'The default image status is permanent.'); 454 $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
454 // Create a new node with no image attached and ensure that default private 455 // Create a new node with no image attached and ensure that default private
455 // image is displayed. 456 // image is displayed.
456 $node = $this->drupalCreateNode(['type' => 'article']); 457 $node = $this->drupalCreateNode(['type' => 'article']);