3 namespace Drupal\Tests\blazy\Kernel;
5 use Drupal\Core\Form\FormState;
6 use Drupal\blazy\BlazyMedia;
9 * Tests the Blazy image formatter.
11 * @coversDefaultClass \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatter
14 class BlazyFormatterTest extends BlazyKernelTestBase {
19 protected function setUp() {
23 'field_video' => 'image',
24 'field_image' => 'image',
29 $bundle = $this->bundle;
30 $this->setUpContentTypeTest($bundle, $data);
32 $data['settings'] = $this->getFormatterSettings();
33 $this->display = $this->setUpFormatterDisplay($bundle, $data);
35 $this->setUpContentWithItems($bundle);
36 $this->setUpRealImage();
38 $this->formatterInstance = $this->getFormatterInstance();
42 * Tests the Blazy formatter methods.
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']);
50 $entity = $this->entity;
51 $build = $this->display->build($entity);
53 $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->testItems, 'Field implements interface.');
54 $this->assertInstanceOf('\Drupal\blazy\BlazyManagerInterface', $this->formatterInstance->blazyManager(), 'BlazyManager implements interface.');
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');
61 $render = $this->blazyManager->getRenderer()->renderRoot($build);
62 $this->assertNotEmpty($render);
64 // Tests ::settingsForm.
66 $definition = $this->getFormatterDefinition();
67 $definition['_views'] = TRUE;
69 // Check for setttings form.
70 $form_state = new FormState();
71 $elements = $this->formatterInstance->settingsForm($form, $form_state);
72 $this->assertArrayHasKey('closing', $elements);
74 $formatter_settings = $this->formatterInstance->buildSettings();
75 $this->assertArrayHasKey('plugin_id', $formatter_settings);
77 // Tests formatter settings.
78 $build = $this->display->build($this->entity);
80 $result = $this->entity->{$this->testFieldName}->view(['type' => 'blazy']);
81 $this->assertEquals('blazy', $result[0]['#theme']);
83 $component = $this->display->getComponent($this->testFieldName);
85 $this->assertEquals($this->testPluginId, $component['type']);
86 $this->assertEquals($this->testPluginId, $build[$this->testFieldName]['#formatter']);
88 $format['settings'] = $this->getFormatterSettings();
90 $settings = &$format['settings'];
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';
103 $settings['vanilla'] = TRUE;
104 $this->blazyFormatterManager->buildSettings($format, $this->testItems);
106 catch (\PHPUnit_Framework_Exception $e) {
109 $this->assertEquals($this->testFieldName, $settings['field_name']);
111 $settings['vanilla'] = FALSE;
112 $this->blazyFormatterManager->buildSettings($format, $this->testItems);
114 $this->assertEquals($this->testFieldName, $settings['field_name']);
115 $this->assertArrayHasKey('#blazy', $build[$this->testFieldName]);
118 // Verify no optionsets without a defined function paramater.
120 $options_1a = $this->blazyAdminFormatter->getOptionsetOptions();
122 catch (\PHPUnit_Framework_Exception $e) {
124 $this->assertEmpty($options_1a);
126 $options_1b = $this->blazyAdminFormatter->getOptionsetOptions('image_style');
127 $this->assertArrayHasKey('large', $options_1b);
130 $new_settings = $this->getFormatterSettings();
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';
139 $this->display->setComponent($this->testFieldName, [
140 'type' => $this->testPluginId,
141 'settings' => $new_settings,
145 $build = $this->display->build($this->entity);
147 // Verify theme_field() is taken over by BlazyGrid::build().
148 $this->assertArrayNotHasKey('#blazy', $build[$this->testFieldName]);
152 * Tests the Blazy formatter file.
154 public function testBlazyFile() {
156 'iframe_lazy' => TRUE,
157 'media_switch' => 'media',
159 'view_mode' => 'default',
163 'field_name' => 'field_image',
164 'plugin_id' => 'blazy_file',
165 'settings' => $settings + $this->getFormatterSettings(),
167 $display = $this->setUpFormatterDisplay($this->bundle, $data);
169 $formatter = $this->getFormatterInstance('blazy_file');
170 $build = $this->display->build($this->entity);
172 $scopes = $formatter->getScopedFormElements();
173 $this->assertArrayHasKey('multimedia', $scopes);
175 $render = $this->blazyManager->getRenderer()->renderRoot($build);
176 $this->assertTrue(strpos($render, 'data-blazy') !== FALSE);
180 * Tests the Blazy formatter faked Media integration.
182 * @param mixed|string|bool $input_url
183 * Input URL, else empty.
184 * @param bool $expected
185 * The expected output.
187 * @dataProvider providerTestBlazyMedia
189 public function testBlazyMedia($input_url, $expected) {
190 $entity = $this->entity;
193 'input_url' => $input_url,
194 'source_field' => $this->testFieldName,
195 'view_mode' => 'default',
196 'bundle' => $this->bundle,
197 'thumbnail_style' => 'thumbnail',
201 $build = $this->display->build($entity);
203 $render = BlazyMedia::build($entity, $settings);
205 if ($expected && $render) {
206 $this->assertNotEmpty($render);
209 $wrap = BlazyMedia::wrap($field, $settings);
210 $this->assertNotEmpty($wrap);
212 $render = $this->blazyManager->getRenderer()->renderRoot($build[$this->testFieldName]);
213 $this->assertTrue(strpos($render, 'data-blazy') !== FALSE);
216 $this->assertFalse($render);
221 * Provide test cases for ::testBlazyMedia().
224 * An array of tested data.
226 public function providerTestBlazyMedia() {
229 ['http://xyz123.com/x/123', FALSE],