4783d0f67ad58e3d2a2d04fa11283be3ba81fe6b
[yaffs-website] / web / modules / contrib / blazy / tests / src / Unit / BlazyMediaUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\blazy\BlazyMedia;
7 use Drupal\Tests\blazy\Traits\BlazyUnitTestTrait;
8
9 /**
10  * @coversDefaultClass \Drupal\blazy\BlazyMedia
11  *
12  * @group blazy
13  */
14 class BlazyMediaUnitTest extends UnitTestCase {
15
16   use BlazyUnitTestTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function setUp() {
22     parent::setUp();
23
24     $this->setUpVariables();
25     $this->setUpUnitImages();
26   }
27
28   /**
29    * Tests \Drupal\blazy\BlazyMedia::build().
30    *
31    * @covers ::build
32    * @covers ::wrap
33    * @dataProvider providerTestBlazyMediaBuild
34    */
35   public function testBlazyMediaBuild($markup) {
36     $settings = [
37       'source_field' => $this->randomMachineName(),
38       'image_style'  => 'blazy_crop',
39       'ratio'        => 'fluid',
40       'view_mode'    => 'default',
41       'bundle'       => 'entity_test',
42     ];
43
44     $markup['#settings'] = $settings;
45     $markup['#attached'] = [];
46     $markup['#cache']    = [];
47
48     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
49     $entity = $this->getMock('Drupal\Core\Entity\ContentEntityInterface');
50     $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
51
52     $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
53     $items->expects($this->any())
54       ->method('getFieldDefinition')
55       ->willReturn($field_definition);
56     $items->expects($this->any())
57       ->method('view')
58       ->with($settings['view_mode'])
59       ->willReturn($markup);
60     $items->expects($this->any())
61       ->method('getEntity')
62       ->willReturn($entity);
63
64     $entity->expects($this->once())
65       ->method('get')
66       ->with($settings['source_field'])
67       ->will($this->returnValue($items));
68
69     $render = BlazyMedia::build($entity, $settings);
70     $this->assertArrayHasKey('#settings', $render);
71   }
72
73   /**
74    * Provider for ::testBlazyMediaBuild.
75    */
76   public function providerTestBlazyMediaBuild() {
77     $iframe = [
78       '#type' => 'html_tag',
79       '#tag' => 'iframe',
80       '#attributes' => [
81         'allowfullscreen' => 'true',
82         'frameborder' => 0,
83         'scrolling' => 'no',
84         'src' => '//www.youtube.com/watch?v=E03HFA923kw',
85         'width' => 640,
86         'height' => 360,
87       ],
88     ];
89
90     $markup['#markup'] = '<iframe src="//www.youtube.com/watch?v=E03HFA923kw" class="b-lazy"></iframe>';
91
92     return [
93       'With children, has iframe tag' => [
94         [$iframe],
95       ],
96       'Without children, has iframe tag' => [
97         $iframe,
98       ],
99       'With children, has no iframe tag' => [
100         [$markup],
101       ],
102     ];
103   }
104
105 }