b120400604f73a1325052e03057f73e4b20676d9
[yaffs-website] / PluginInstanceTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests that an instance of all views plugins can be created.
9  *
10  * @group views
11  */
12 class PluginInstanceTest extends ViewsKernelTestBase {
13
14   /**
15    * All views plugin types.
16    *
17    * @var array
18    */
19   protected $pluginTypes = [
20     'access',
21     'area',
22     'argument',
23     'argument_default',
24     'argument_validator',
25     'cache',
26     'display_extender',
27     'display',
28     'exposed_form',
29     'field',
30     'filter',
31     'join',
32     'pager',
33     'query',
34     'relationship',
35     'row',
36     'sort',
37     'style',
38     'wizard',
39   ];
40
41   /**
42    * An array of plugin definitions, keyed by plugin type.
43    *
44    * @var array
45    */
46   protected $definitions;
47
48   protected function setUp($import_test_views = TRUE) {
49     parent::setUp();
50
51     $this->definitions = Views::getPluginDefinitions();
52   }
53
54   /**
55    * Confirms that there is plugin data for all views plugin types.
56    */
57   public function testPluginData() {
58     // Check that we have an array of data.
59     $this->assertTrue(is_array($this->definitions), 'Plugin data is an array.');
60
61     // Check all plugin types.
62     foreach ($this->pluginTypes as $type) {
63       $this->assertTrue(array_key_exists($type, $this->definitions), format_string('Key for plugin type @type found.', ['@type' => $type]));
64       $this->assertTrue(is_array($this->definitions[$type]) && !empty($this->definitions[$type]), format_string('Plugin type @type has an array of plugins.', ['@type' => $type]));
65     }
66
67     // Tests that the plugin list has not missed any types.
68     $diff = array_diff(array_keys($this->definitions), $this->pluginTypes);
69     $this->assertTrue(empty($diff), 'All plugins were found and matched.');
70   }
71
72   /**
73    * Tests creating instances of every views plugin.
74    *
75    * This will iterate through all plugins from _views_fetch_plugin_data().
76    */
77   public function testPluginInstances() {
78     foreach ($this->definitions as $type => $plugins) {
79       // Get a plugin manager for this type.
80       $manager = $this->container->get("plugin.manager.views.$type");
81       foreach ($plugins as $id => $definition) {
82         // Get a reflection class for this plugin.
83         // We only want to test true plugins, i.e. They extend PluginBase.
84         $reflection = new \ReflectionClass($definition['class']);
85         if ($reflection->isSubclassOf('Drupal\views\Plugin\views\PluginBase')) {
86           // Create a plugin instance and check what it is. This is not just
87           // good to check they can be created but for throwing any notices for
88           // method signatures etc. too.
89           $instance = $manager->createInstance($id);
90           $this->assertTrue($instance instanceof $definition['class'], format_string('Instance of @type:@id created', ['@type' => $type, '@id' => $id]));
91         }
92       }
93     }
94   }
95
96 }