PluginInstanceTest.php 2.8 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Kernel;
4

5 6
use Drupal\views\Views;

7
/**
8 9 10
 * Tests that an instance of all views plugins can be created.
 *
 * @group views
11
 */
12
class PluginInstanceTest extends ViewsKernelTestBase {
13 14 15 16 17 18

  /**
   * All views plugin types.
   *
   * @var array
   */
19
  protected $pluginTypes = array(
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    'access',
    'area',
    'argument',
    'argument_default',
    'argument_validator',
    'cache',
    'display_extender',
    'display',
    'exposed_form',
    'field',
    'filter',
    'join',
    'pager',
    'query',
    'relationship',
    'row',
    'sort',
    'style',
    'wizard',
  );

41 42 43 44 45 46 47
  /**
   * An array of plugin definitions, keyed by plugin type.
   *
   * @var array
   */
  protected $definitions;

48
  protected function setUp($import_test_views = TRUE) {
49 50
    parent::setUp();

51
    $this->definitions = Views::getPluginDefinitions();
52 53
  }

54 55 56 57
  /**
   * Confirms that there is plugin data for all views plugin types.
   */
  public function testPluginData() {
58 59
    // Check that we have an array of data.
    $this->assertTrue(is_array($this->definitions), 'Plugin data is an array.');
60 61

    // Check all plugin types.
62 63 64
    foreach ($this->pluginTypes as $type) {
      $this->assertTrue(array_key_exists($type, $this->definitions), format_string('Key for plugin type @type found.', array('@type' => $type)));
      $this->assertTrue(is_array($this->definitions[$type]) && !empty($this->definitions[$type]), format_string('Plugin type @type has an array of plugins.', array('@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 70 71 72 73 74 75 76 77
    $this->assertTrue(empty($diff), 'All plugins were found and matched.');
  }

  /**
   * Tests creating instances of every views plugin.
   *
   * This will iterate through all plugins from _views_fetch_plugin_data().
   */
  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 83
        // Get a reflection class for this plugin.
        // We only want to test true plugins, i.e. They extend PluginBase.
84
        $reflection = new \ReflectionClass($definition['class']);
85 86 87
        if ($reflection->isSubclassOf('Drupal\views\Plugin\views\PluginBase')) {
          // Create a plugin instance and check what it is. This is not just
          // good to check they can be created but for throwing any notices for
88
          // method signatures etc. too.
89 90
          $instance = $manager->createInstance($id);
          $this->assertTrue($instance instanceof $definition['class'], format_string('Instance of @type:@id created', array('@type' => $type, '@id' => $id)));
91 92 93 94 95 96
        }
      }
    }
  }

}