bb4549831d693d90f155973e5e9b35fdbabd9782
[yaffs-website] / web / modules / contrib / blazy / tests / src / Kernel / BlazyFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Kernel;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\blazy\BlazyMedia;
7
8 /**
9  * Tests the Blazy image formatter.
10  *
11  * @coversDefaultClass \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatter
12  * @group blazy
13  */
14 class BlazyFormatterTest extends BlazyKernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     parent::setUp();
21
22     $data['fields'] = [
23       'field_video' => 'image',
24       'field_image' => 'image',
25       'field_id'    => 'text',
26     ];
27
28     // Create contents.
29     $bundle = $this->bundle;
30     $this->setUpContentTypeTest($bundle, $data);
31
32     $data['settings'] = $this->getFormatterSettings();
33     $this->display = $this->setUpFormatterDisplay($bundle, $data);
34
35     $this->setUpContentWithItems($bundle);
36     $this->setUpRealImage();
37
38     $this->formatterInstance = $this->getFormatterInstance();
39   }
40
41   /**
42    * Tests the Blazy formatter methods.
43    */
44   public function testBlazyFormatterMethods() {
45     // Tests type definition.
46     $this->typeDefinition = $this->blazyAdminFormatter->getTypedConfig()->getDefinition('blazy.settings');
47     $this->assertEquals('Blazy settings', $this->typeDefinition['label']);
48
49     // Tests cache.
50     $entity = $this->entity;
51     $build = $this->display->build($entity);
52
53     $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->testItems, 'Field implements interface.');
54     $this->assertInstanceOf('\Drupal\blazy\BlazyManagerInterface', $this->formatterInstance->blazyManager(), 'BlazyManager implements interface.');
55
56     // Tests cache tags matching entity ::getCacheTags().
57     $item = $entity->{$this->testFieldName};
58     $this->assertEquals($item[0]->entity->getCacheTags(), $build[$this->testFieldName][0]['#build']['settings']['file_tags'], 'First image cache tags is as expected');
59     $this->assertEquals($item[1]->entity->getCacheTags(), $build[$this->testFieldName][1]['#build']['settings']['file_tags'], 'Second image cache tags is as expected');
60
61     $render = $this->blazyManager->getRenderer()->renderRoot($build);
62     $this->assertNotEmpty($render);
63
64     // Tests ::settingsForm.
65     $form = [];
66     $definition = $this->getFormatterDefinition();
67     $definition['_views'] = TRUE;
68
69     // Check for setttings form.
70     $form_state = new FormState();
71     $elements = $this->formatterInstance->settingsForm($form, $form_state);
72     $this->assertArrayHasKey('closing', $elements);
73
74     $formatter_settings = $this->formatterInstance->buildSettings();
75     $this->assertArrayHasKey('plugin_id', $formatter_settings);
76
77     // Tests formatter settings.
78     $build = $this->display->build($this->entity);
79
80     $result = $this->entity->{$this->testFieldName}->view(['type' => 'blazy']);
81     $this->assertEquals('blazy', $result[0]['#theme']);
82
83     $component = $this->display->getComponent($this->testFieldName);
84
85     $this->assertEquals($this->testPluginId, $component['type']);
86     $this->assertEquals($this->testPluginId, $build[$this->testFieldName]['#formatter']);
87
88     $format['settings'] = $this->getFormatterSettings();
89
90     $settings = &$format['settings'];
91
92     $settings['breakpoints']     = $this->getDataBreakpoints(TRUE);
93     $settings['bundle']          = $this->bundle;
94     $settings['blazy']           = TRUE;
95     $settings['grid']            = 0;
96     $settings['lazy']            = 'blazy';
97     $settings['background']      = TRUE;
98     $settings['thumbnail_style'] = 'thumbnail';
99     $settings['ratio']           = 'enforced';
100     $settings['image_style']     = 'blazy_crop';
101
102     try {
103       $settings['vanilla'] = TRUE;
104       $this->blazyFormatterManager->buildSettings($format, $this->testItems);
105     }
106     catch (\PHPUnit_Framework_Exception $e) {
107     }
108
109     $this->assertEquals($this->testFieldName, $settings['field_name']);
110
111     $settings['vanilla'] = FALSE;
112     $this->blazyFormatterManager->buildSettings($format, $this->testItems);
113
114     $this->assertEquals($this->testFieldName, $settings['field_name']);
115     $this->assertArrayHasKey('#blazy', $build[$this->testFieldName]);
116
117     // Tests options.
118     // Verify no optionsets without a defined function paramater.
119     try {
120       $options_1a = $this->blazyAdminFormatter->getOptionsetOptions();
121     }
122     catch (\PHPUnit_Framework_Exception $e) {
123     }
124     $this->assertEmpty($options_1a);
125
126     $options_1b = $this->blazyAdminFormatter->getOptionsetOptions('image_style');
127     $this->assertArrayHasKey('large', $options_1b);
128
129     // Tests grid.
130     $new_settings = $this->getFormatterSettings();
131
132     $new_settings['grid']         = 4;
133     $new_settings['grid_medium']  = 3;
134     $new_settings['grid_small']   = 2;
135     $new_settings['media_switch'] = 'blazy_test';
136     $new_settings['style']        = 'column';
137     $new_settings['image_style']  = 'blazy_crop';
138
139     $this->display->setComponent($this->testFieldName, [
140       'type'     => $this->testPluginId,
141       'settings' => $new_settings,
142       'label'    => 'hidden',
143     ]);
144
145     $build = $this->display->build($this->entity);
146
147     // Verify theme_field() is taken over by BlazyGrid::build().
148     $this->assertArrayNotHasKey('#blazy', $build[$this->testFieldName]);
149   }
150
151   /**
152    * Tests the Blazy formatter file.
153    */
154   public function testBlazyFile() {
155     $settings = [
156       'iframe_lazy'  => TRUE,
157       'media_switch' => 'media',
158       'ratio'        => 'fluid',
159       'view_mode'    => 'default',
160     ];
161
162     $data = [
163       'field_name' => 'field_image',
164       'plugin_id'  => 'blazy_file',
165       'settings'   => $settings + $this->getFormatterSettings(),
166     ];
167     $display = $this->setUpFormatterDisplay($this->bundle, $data);
168
169     $formatter = $this->getFormatterInstance('blazy_file');
170     $build = $this->display->build($this->entity);
171
172     $scopes = $formatter->getScopedFormElements();
173     $this->assertArrayHasKey('multimedia', $scopes);
174
175     $render = $this->blazyManager->getRenderer()->renderRoot($build);
176     $this->assertTrue(strpos($render, 'data-blazy') !== FALSE);
177   }
178
179   /**
180    * Tests the Blazy formatter faked Media integration.
181    *
182    * @param mixed|string|bool $input_url
183    *   Input URL, else empty.
184    * @param bool $expected
185    *   The expected output.
186    *
187    * @dataProvider providerTestBlazyMedia
188    */
189   public function testBlazyMedia($input_url, $expected) {
190     $entity = $this->entity;
191
192     $settings = [
193       'input_url'       => $input_url,
194       'source_field'    => $this->testFieldName,
195       'view_mode'       => 'default',
196       'bundle'          => $this->bundle,
197       'thumbnail_style' => 'thumbnail',
198       'uri'             => $this->uri,
199     ];
200
201     $build = $this->display->build($entity);
202
203     $render = BlazyMedia::build($entity, $settings);
204
205     if ($expected && $render) {
206       $this->assertNotEmpty($render);
207
208       $field[0] = $render;
209       $wrap = BlazyMedia::wrap($field, $settings);
210       $this->assertNotEmpty($wrap);
211
212       $render = $this->blazyManager->getRenderer()->renderRoot($build[$this->testFieldName]);
213       $this->assertTrue(strpos($render, 'data-blazy') !== FALSE);
214     }
215     else {
216       $this->assertFalse($render);
217     }
218   }
219
220   /**
221    * Provide test cases for ::testBlazyMedia().
222    *
223    * @return array
224    *   An array of tested data.
225    */
226   public function providerTestBlazyMedia() {
227     return [
228       ['', TRUE],
229       ['http://xyz123.com/x/123', FALSE],
230       ['user', TRUE],
231     ];
232   }
233
234 }