Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / modules / contrib / blazy / tests / src / Kernel / Views / BlazyViewsFileTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Kernel\Views;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\views\Views;
7 use Drupal\blazy\BlazyViews;
8
9 /**
10  * Test Blazy Views integration.
11  *
12  * @coversDefaultClass \Drupal\blazy\Dejavu\BlazyStylePluginBase
13  * @group blazy
14  */
15 class BlazyViewsFileTest extends BlazyViewsTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $testViews = ['test_blazy_entity', 'test_blazy_entity_2'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp($import_test_views = TRUE) {
26     parent::setUp($import_test_views);
27
28     $this->entityFieldName = 'field_entity_test';
29     $this->entityPluginId  = 'blazy_entity_test';
30     $this->targetBundle    = 'bundle_target_test';
31     $this->targetBundles   = [$this->targetBundle];
32
33     $bundle = $this->bundle;
34     $settings['image_settings'] = [
35       'iframe_lazy'  => TRUE,
36       'image_style'  => 'blazy_crop',
37       'media_switch' => 'blazy_test',
38       'ratio'        => 'fluid',
39       'view_mode'    => 'default',
40     ];
41
42     $this->setUpContentWithEntityReference($settings);
43
44     // Create referencing entity.
45     $this->referencingEntity = $this->createReferencingEntity();
46
47     $data['fields'] = $this->getDefaultFields();
48
49     // Create contents.
50     $this->setUpContentTypeTest($bundle, $data);
51
52     $data['settings'] = $this->getFormatterSettings();
53     $data['settings']['breakpoints'] = $this->getDataBreakpoints(TRUE);
54
55     $display = $this->setUpFormatterDisplay($bundle, $data);
56
57     $display->setComponent('field_image', [
58       'type'     => 'blazy',
59       'settings' => $settings['image_settings'],
60       'label'    => 'hidden',
61     ]);
62
63     $display->save();
64
65     $this->setUpContentWithItems($bundle);
66   }
67
68   /**
69    * Make sure that the HTML list style markup is correct.
70    */
71   public function testBlazyViews() {
72     $view = Views::getView('test_blazy_entity');
73     $this->executeView($view);
74     $view->setDisplay('default');
75
76     $style_plugin = $view->style_plugin;
77
78     $this->assertInstanceOf('\Drupal\blazy\BlazyManagerInterface', $style_plugin->blazyManager(), 'BlazyManager implements interface.');
79     $this->assertInstanceOf('\Drupal\blazy_test\Form\BlazyAdminTestInterface', $style_plugin->admin(), 'BlazyAdmin implements interface.');
80
81     $style_plugin->options                            = array_merge($style_plugin->options, $this->getDefaultFields(TRUE));
82     $style_plugin->options['grid']                    = 0;
83     $style_plugin->options['grid_medium']             = 3;
84     $style_plugin->options['grid_small']              = 1;
85     $style_plugin->options['image']                   = 'field_image';
86     $style_plugin->options['media_switch']            = 'blazy_test';
87     $style_plugin->options['overlay']                 = $this->testFieldName;
88     $style_plugin->options['caption']['title']        = 'title';
89     $style_plugin->options['caption']['field_teaser'] = 'field_teaser';
90
91     $settings = $style_plugin->options;
92
93     // Forms.
94     $fields = [
95       'captions',
96       'layouts',
97       'images',
98       'links',
99       'titles',
100       'classes',
101       'overlays',
102       'thumbnails',
103       'layouts',
104     ];
105     $definition = $style_plugin->getDefinedFieldOptions($fields);
106     $this->assertEquals('blazy_test', $definition['plugin_id']);
107
108     $form = [];
109     $form_state = new FormState();
110     $style_plugin->buildOptionsForm($form, $form_state);
111     $this->assertArrayHasKey('closing', $form);
112
113     $style_plugin->submitOptionsForm($form, $form_state);
114
115     // @todo: Fields.
116     $image = [];
117     $index = 0;
118     $row = $view->result[$index];
119
120     // Render.
121     $render = $view->getStyle()->render();
122     $this->assertArrayHasKey('data-blazy', $render['#attributes']);
123
124     $output = $view->preview();
125     $output = $this->blazyManager->getRenderer()->renderRoot($output);
126     $this->assertTrue(strpos($output, 'data-blazy') !== FALSE, 'Blazy attribute is added to DIV.');
127
128     $element = ['settings' => $settings];
129     $view->getStyle()->buildElement($element, $row, $index);
130
131     try {
132       $output = $view->getStyle()->getImageRenderable($settings, $row, $index);
133     }
134     catch (\PHPUnit_Framework_Exception $e) {
135     }
136     $this->assertTrue(TRUE);
137
138     $image = $view->getStyle()->getImageRenderable($settings, $row, $index);
139     $this->assertArrayHasKey('image', $settings);
140
141     $output = $view->getStyle()->getImageItem($image);
142     $this->assertArrayHasKey('image', $settings);
143
144     $output = $view->getStyle()->isImageRenderable($row, $index, $this->testFieldName);
145     $this->assertArrayHasKey('image', $settings);
146
147     $output = $view->getStyle()->getCaption($index, $settings);
148     $this->assertArrayHasKey('caption', $settings);
149
150     $view->getStyle()->getLayout($settings, $index);
151     $this->assertArrayHasKey('layout', $settings);
152
153     $output = FALSE;
154     try {
155       $output = $view->getStyle()->getFieldRenderable($row, $index, '');
156     }
157     catch (\PHPUnit_Framework_Exception $e) {
158     }
159
160     $this->assertTrue(TRUE);
161
162     $output = $view->getStyle()->getFieldRendered($index, $this->testFieldName);
163     $this->assertArrayHasKey('image', $settings);
164
165     $output = $view->getStyle()->getFieldRenderable($row, $index, $this->testFieldName);
166     $this->assertArrayHasKey('image', $settings);
167
168     $output = $view->getStyle()->getFieldString($row, 'title', $index);
169     $this->assertNotEmpty($output[0]);
170
171     if ($blazy = BlazyViews::viewsField($view)) {
172       $scopes = $blazy->getScopedFormElements();
173       $this->assertArrayHasKey('settings', $scopes);
174
175       $form = [];
176       $form_state = new FormState();
177       $blazy->buildOptionsForm($form, $form_state);
178       $this->assertArrayHasKey('image_style', $form);
179
180       $this->assertInstanceOf('\Drupal\blazy\Form\BlazyAdminInterface', $blazy->blazyAdmin(), 'BlazyAdmin implements interface.');
181     }
182     $view->destroy();
183
184     $view = Views::getView('test_blazy_entity_2');
185     $this->executeView($view);
186     $view->setDisplay('default');
187
188     $style_plugin = $view->style_plugin;
189     $style_plugin->options['grid'] = 0;
190
191     $form = [];
192     $form_state = new FormState();
193     $style_plugin->buildOptionsForm($form, $form_state);
194     $this->assertArrayHasKey('closing', $form);
195
196     $style_plugin->submitOptionsForm($form, $form_state);
197
198     // Render.
199     $render = $view->getStyle()->render();
200     $this->assertArrayHasKey('data-blazy', $render['#attributes']);
201
202     $output = $view->preview();
203     $output = $this->blazyManager->getRenderer()->renderRoot($output);
204     $this->assertTrue(strpos($output, 'data-blazy') !== FALSE, 'Blazy attribute is added to DIV.');
205     $view->destroy();
206   }
207
208 }