Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / DisplayKernelTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views\Views;
6 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
7 use Drupal\views\Plugin\views\style\StylePluginBase;
8 use Drupal\views\Plugin\views\access\AccessPluginBase;
9 use Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface;
10 use Drupal\views\Plugin\views\pager\PagerPluginBase;
11 use Drupal\views\Plugin\views\query\QueryPluginBase;
12 use Drupal\views\Plugin\views\cache\CachePluginBase;
13 use Drupal\views\Plugin\views\row\RowPluginBase;
14
15 /**
16  * Drupal unit tests for the DisplayPluginBase class.
17  *
18  * @group views
19  */
20 class DisplayKernelTest extends ViewsKernelTestBase {
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['block', 'node', 'field', 'user'];
28
29   /**
30    * Views plugin types to test.
31    *
32    * @var array
33    */
34   protected $pluginTypes = [
35     'access',
36     'cache',
37     'query',
38     'exposed_form',
39     'pager',
40     'style',
41     'row',
42   ];
43
44   /**
45    * Views handler types to test.
46    *
47    * @var array
48    */
49   protected $handlerTypes = [
50     'fields',
51     'sorts',
52   ];
53
54   /**
55    * Views used by this test.
56    *
57    * @var array
58    */
59   public static $testViews = ['test_display_defaults'];
60
61   /**
62    * Tests the default display options.
63    */
64   public function testDefaultOptions() {
65     // Save the view.
66     $view = Views::getView('test_display_defaults');
67     $view->mergeDefaults();
68     $view->save();
69
70     // Reload to get saved storage values.
71     $view = Views::getView('test_display_defaults');
72     $view->initDisplay();
73     $display_data = $view->storage->get('display');
74
75     foreach ($view->displayHandlers as $id => $display) {
76       // Test the view plugin options against the storage.
77       foreach ($this->pluginTypes as $type) {
78         $options = $display->getOption($type);
79         $this->assertIdentical($display_data[$id]['display_options'][$type]['options'], $options['options']);
80       }
81       // Test the view handler options against the storage.
82       foreach ($this->handlerTypes as $type) {
83         $options = $display->getOption($type);
84         $this->assertIdentical($display_data[$id]['display_options'][$type], $options);
85       }
86     }
87   }
88
89   /**
90    * Tests the \Drupal\views\Plugin\views\display\DisplayPluginBase::getPlugin() method.
91    */
92   public function testGetPlugin() {
93     $view = Views::getView('test_display_defaults');
94     $view->initDisplay();
95     $display_handler = $view->display_handler;
96
97     $this->assertTrue($display_handler->getPlugin('access') instanceof AccessPluginBase, 'An access plugin instance was returned.');
98     $this->assertTrue($display_handler->getPlugin('cache') instanceof CachePluginBase, 'A cache plugin instance was returned.');
99     $this->assertTrue($display_handler->getPlugin('exposed_form') instanceof ExposedFormPluginInterface, 'An exposed_form plugin instance was returned.');
100     $this->assertTrue($display_handler->getPlugin('pager') instanceof PagerPluginBase, 'A pager plugin instance was returned.');
101     $this->assertTrue($display_handler->getPlugin('query') instanceof QueryPluginBase, 'A query plugin instance was returned.');
102     $this->assertTrue($display_handler->getPlugin('row') instanceof RowPluginBase, 'A row plugin instance was returned.');
103     $this->assertTrue($display_handler->getPlugin('style') instanceof StylePluginBase, 'A style plugin instance was returned.');
104     // Test that nothing is returned when an invalid type is requested.
105     $this->assertNull($display_handler->getPlugin('invalid'), 'NULL was returned for an invalid instance');
106     // Test that nothing was returned for an instance with no 'type' in options.
107     unset($display_handler->options['access']);
108     $this->assertNull($display_handler->getPlugin('access'), 'NULL was returned for a plugin type with no "type" option');
109
110     // Get a plugin twice, and make sure the same instance is returned.
111     $view->destroy();
112     $view->initDisplay();
113     $first = spl_object_hash($display_handler->getPlugin('style'));
114     $second = spl_object_hash($display_handler->getPlugin('style'));
115     $this->assertIdentical($first, $second, 'The same plugin instance was returned.');
116   }
117
118 }