3 namespace Drupal\Tests\views\Kernel;
5 use Drupal\views\Views;
8 * Tests that an instance of all views plugins can be created.
12 class PluginInstanceTest extends ViewsKernelTestBase {
15 * All views plugin types.
19 protected $pluginTypes = [
42 * An array of plugin definitions, keyed by plugin type.
46 protected $definitions;
48 protected function setUp($import_test_views = TRUE) {
51 $this->definitions = Views::getPluginDefinitions();
55 * Confirms that there is plugin data for all views plugin types.
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.');
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]));
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.');
73 * Tests creating instances of every views plugin.
75 * This will iterate through all plugins from _views_fetch_plugin_data().
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]));