PluginInstanceTest.php 3.06 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\PluginInstanceTest.
6 7
 */

8
namespace Drupal\views\Tests;
9 10 11 12

/**
 * Checks general plugin data and instances for all plugin types.
 */
13
class PluginInstanceTest extends ViewTestBase {
14 15 16 17 18 19

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

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

49 50
  public static function getInfo() {
    return array(
51
      'name' => 'Plugin instantiation',
52
      'description' => 'Tests that an instance of all views plugins can be created.',
53
      'group' => 'Views',
54 55 56
    );
  }

57 58 59 60 61 62
  protected function setUp() {
    parent::setUp();

    $this->definitions = views_get_plugin_definitions();
  }

63 64 65 66
  /**
   * Confirms that there is plugin data for all views plugin types.
   */
  public function testPluginData() {
67 68
    // Check that we have an array of data.
    $this->assertTrue(is_array($this->definitions), 'Plugin data is an array.');
69 70

    // Check all plugin types.
71 72 73
    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)));
74 75 76
    }

    // Tests that the plugin list has not missed any types.
77
    $diff = array_diff(array_keys($this->definitions), $this->pluginTypes);
78 79 80 81 82 83 84 85 86
    $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() {
87
    $container = drupal_container();
88
    foreach ($this->definitions as $type => $plugins) {
89
      // Get a plugin manager for this type.
90
      $manager = $container->get("plugin.manager.views.$type");
91 92 93
      foreach ($plugins as $definition) {
        // Get a reflection class for this plugin.
        // We only want to test true plugins, i.e. They extend PluginBase.
94
        $reflection = new \ReflectionClass($definition['class']);
95 96 97 98 99 100 101 102 103 104 105 106
        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
          // method signatures etc... too.
          $instance = $manager->createInstance($definition['id']);
          $this->assertTrue($instance instanceof $definition['class'], format_string('Instance of @type:@id created', array('@type' => $type, '@id' => $definition['id'])));
        }
      }
    }
  }

}