ImageTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Drupal\KernelTests\Core\Theme;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Symfony\Component\HttpFoundation\Request;
  5. /**
  6. * Tests built-in image theme functions.
  7. *
  8. * @group Theme
  9. */
  10. class ImageTest extends KernelTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['system'];
  17. /*
  18. * The images to test with.
  19. *
  20. * @var array
  21. */
  22. protected $testImages;
  23. protected function setUp() {
  24. parent::setUp();
  25. // The code under test uses file_url_transform_relative(), which relies on
  26. // the Request containing the correct hostname. KernelTestBase doesn't set
  27. // it, so push another request onto the stack to ensure it's correct.
  28. $request = Request::create('/', 'GET', [], [], [], $_SERVER);
  29. $this->container = \Drupal::service('kernel')->getContainer();
  30. $this->container->get('request_stack')->push($request);
  31. $this->testImages = [
  32. 'core/misc/druplicon.png',
  33. 'core/misc/loading.gif',
  34. ];
  35. }
  36. /**
  37. * Tests that an image with the sizes attribute is output correctly.
  38. */
  39. public function testThemeImageWithSizes() {
  40. // Test with multipliers.
  41. $sizes = '(max-width: ' . rand(10, 30) . 'em) 100vw, (max-width: ' . rand(30, 50) . 'em) 50vw, 30vw';
  42. $image = [
  43. '#theme' => 'image',
  44. '#sizes' => $sizes,
  45. '#uri' => reset($this->testImages),
  46. '#width' => rand(0, 1000) . 'px',
  47. '#height' => rand(0, 500) . 'px',
  48. '#alt' => $this->randomMachineName(),
  49. '#title' => $this->randomMachineName(),
  50. ];
  51. $this->render($image);
  52. // Make sure sizes is set.
  53. $this->assertRaw($sizes, 'Sizes is set correctly.');
  54. }
  55. /**
  56. * Tests that an image with the src attribute is output correctly.
  57. */
  58. public function testThemeImageWithSrc() {
  59. $image = [
  60. '#theme' => 'image',
  61. '#uri' => reset($this->testImages),
  62. '#width' => rand(0, 1000) . 'px',
  63. '#height' => rand(0, 500) . 'px',
  64. '#alt' => $this->randomMachineName(),
  65. '#title' => $this->randomMachineName(),
  66. ];
  67. $this->render($image);
  68. // Make sure the src attribute has the correct value.
  69. $this->assertRaw(file_url_transform_relative(file_create_url($image['#uri'])), 'Correct output for an image with the src attribute.');
  70. }
  71. /**
  72. * Tests that an image with the srcset and multipliers is output correctly.
  73. */
  74. public function testThemeImageWithSrcsetMultiplier() {
  75. // Test with multipliers.
  76. $image = [
  77. '#theme' => 'image',
  78. '#srcset' => [
  79. [
  80. 'uri' => $this->testImages[0],
  81. 'multiplier' => '1x',
  82. ],
  83. [
  84. 'uri' => $this->testImages[1],
  85. 'multiplier' => '2x',
  86. ],
  87. ],
  88. '#width' => rand(0, 1000) . 'px',
  89. '#height' => rand(0, 500) . 'px',
  90. '#alt' => $this->randomMachineName(),
  91. '#title' => $this->randomMachineName(),
  92. ];
  93. $this->render($image);
  94. // Make sure the srcset attribute has the correct value.
  95. $this->assertRaw(file_url_transform_relative(file_create_url($this->testImages[0])) . ' 1x, ' . file_url_transform_relative(file_create_url($this->testImages[1])) . ' 2x', 'Correct output for image with srcset attribute and multipliers.');
  96. }
  97. /**
  98. * Tests that an image with the srcset and widths is output correctly.
  99. */
  100. public function testThemeImageWithSrcsetWidth() {
  101. // Test with multipliers.
  102. $widths = [
  103. rand(0, 500) . 'w',
  104. rand(500, 1000) . 'w',
  105. ];
  106. $image = [
  107. '#theme' => 'image',
  108. '#srcset' => [
  109. [
  110. 'uri' => $this->testImages[0],
  111. 'width' => $widths[0],
  112. ],
  113. [
  114. 'uri' => $this->testImages[1],
  115. 'width' => $widths[1],
  116. ],
  117. ],
  118. '#width' => rand(0, 1000) . 'px',
  119. '#height' => rand(0, 500) . 'px',
  120. '#alt' => $this->randomMachineName(),
  121. '#title' => $this->randomMachineName(),
  122. ];
  123. $this->render($image);
  124. // Make sure the srcset attribute has the correct value.
  125. $this->assertRaw(file_url_transform_relative(file_create_url($this->testImages[0])) . ' ' . $widths[0] . ', ' . file_url_transform_relative(file_create_url($this->testImages[1])) . ' ' . $widths[1], 'Correct output for image with srcset attribute and width descriptors.');
  126. }
  127. }