class PluginBaseTest

Same name in this branch
  1. 11.x core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php \Drupal\Tests\views\Kernel\Plugin\PluginBaseTest
  2. 11.x core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php \Drupal\Tests\Component\Plugin\PluginBaseTest
Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Unit/PluginBaseTest.php \Drupal\Tests\views\Unit\PluginBaseTest
  2. 9 core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php \Drupal\Tests\views\Kernel\Plugin\PluginBaseTest
  3. 9 core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php \Drupal\Tests\Component\Plugin\PluginBaseTest
  4. 8.9.x core/modules/views/tests/src/Unit/PluginBaseTest.php \Drupal\Tests\views\Unit\PluginBaseTest
  5. 8.9.x core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php \Drupal\Tests\views\Kernel\Plugin\PluginBaseTest
  6. 8.9.x core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php \Drupal\Tests\Component\Plugin\PluginBaseTest
  7. 10 core/modules/views/tests/src/Unit/PluginBaseTest.php \Drupal\Tests\views\Unit\PluginBaseTest
  8. 10 core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php \Drupal\Tests\views\Kernel\Plugin\PluginBaseTest
  9. 10 core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php \Drupal\Tests\Component\Plugin\PluginBaseTest

@coversDefaultClass \Drupal\views\Plugin\views\PluginBase
@group views

Hierarchy

Expanded class hierarchy of PluginBaseTest

File

core/modules/views/tests/src/Unit/PluginBaseTest.php, line 14

Namespace

Drupal\Tests\views\Unit
View source
class PluginBaseTest extends UnitTestCase {
  
  /**
   * The test helper plugin to use for the tests.
   *
   * @var \Drupal\views\Tests\TestHelperPlugin
   */
  protected $testHelperPlugin;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->testHelperPlugin = new TestHelperPlugin([], 'default', []);
  }
  
  /**
   * Tests the unpackOptions method.
   *
   * @param array $storage
   *   The storage array to unpack option into.
   * @param array $options
   *   The array of options to unpack.
   * @param array $definition
   *   The definition array, defining default options.
   * @param array $expected
   *   The expected array after unpacking.
   * @param bool $all
   *   Whether to unpack all options.
   *
   * @dataProvider providerTestUnpackOptions
   * @covers ::unpackOptions
   */
  public function testUnpackOptions($storage, $options, $definition, $expected, $all = FALSE) : void {
    $this->testHelperPlugin
      ->unpackOptions($storage, $options, $definition, $all);
    $this->assertEquals($storage, $expected);
  }
  
  /**
   * Tests the setOptionDefault method.
   *
   * @param array $storage
   *   The storage array to unpack option into.
   * @param array $definition
   *   The definition array, defining default options.
   * @param array $expected
   *   The expected array after unpacking.
   *
   * @dataProvider providerTestSetOptionDefault
   * @covers ::setOptionDefaults
   */
  public function testSetOptionDefault($storage, $definition, $expected) : void {
    $this->testHelperPlugin
      ->testSetOptionDefaults($storage, $definition);
    $this->assertEquals($storage, $expected);
  }
  
  /**
   * Data provider for testUnpackOptions().
   *
   * @return array
   *   An array of test data.
   */
  public static function providerTestUnpackOptions() {
    $test_parameters = [];
    // Set a storage but no value, so the storage value should be kept.
    $test_parameters[] = [
      'storage' => [
        'key' => 'value',
      ],
      'options' => [],
      'definition' => [
        'key' => [
          'default' => 'value2',
        ],
      ],
      'expected' => [
        'key' => 'value',
      ],
    ];
    // Set a storage and an option value, so the option value should be kept.
    $test_parameters[] = [
      'storage' => [
        'key' => 'value',
      ],
      'options' => [
        'key' => 'value2',
      ],
      'definition' => [
        'key' => [
          'default' => 'value3',
        ],
      ],
      'expected' => [
        'key' => 'value2',
      ],
    ];
    // Set no storage but an options value, so the options value should be kept.
    $test_parameters[] = [
      'storage' => [],
      'options' => [
        'key' => 'value',
      ],
      'definition' => [
        'key' => [
          'default' => 'value2',
        ],
      ],
      'expected' => [
        'key' => 'value',
      ],
    ];
    // Set additional options, which aren't part of the definition, so they
    // should be ignored if all is set.
    $test_parameters[] = [
      'storage' => [],
      'options' => [
        'key' => 'value',
        'key2' => 'value2',
      ],
      'definition' => [
        'key' => [
          'default' => 'value2',
        ],
      ],
      'expected' => [
        'key' => 'value',
      ],
    ];
    $test_parameters[] = [
      'storage' => [],
      'options' => [
        'key' => 'value',
        'key2' => 'value2',
      ],
      'definition' => [
        'key' => [
          'default' => 'value2',
        ],
      ],
      'expected' => [
        'key' => 'value',
        'key2' => 'value2',
      ],
      'all' => TRUE,
    ];
    // Provide multiple options with their corresponding definition.
    $test_parameters[] = [
      'storage' => [],
      'options' => [
        'key' => 'value',
        'key2' => 'value2',
      ],
      'definition' => [
        'key' => [
          'default' => 'value2',
        ],
        'key2' => [
          'default' => 'value3',
        ],
      ],
      'expected' => [
        'key' => 'value',
        'key2' => 'value2',
      ],
    ];
    // Set a complex definition structure with a zero and a one level structure.
    $test_parameters[] = [
      'storage' => [],
      'options' => [
        'key0' => 'value',
        'key1' => [
          'key1:1' => 'value1',
          'key1:2' => 'value2',
        ],
      ],
      'definition' => [
        'key0' => [
          'default' => 'value0',
        ],
        'key1' => [
          'contains' => [
            'key1:1' => [
              'default' => 'value1:1',
            ],
          ],
        ],
      ],
      'expected' => [
        'key0' => 'value',
        'key1' => [
          'key1:1' => 'value1',
        ],
      ],
    ];
    // Setup a two level structure.
    $test_parameters[] = [
      'storage' => [],
      'options' => [
        'key2' => [
          'key2:1' => [
            'key2:1:1' => 'value0',
            'key2:1:2' => [
              'key2:1:2:1' => 'value1',
            ],
          ],
        ],
      ],
      'definition' => [
        'key2' => [
          'contains' => [
            'key2:1' => [
              'contains' => [
                'key2:1:1' => [
                  'default' => 'value2:1:2:1',
                ],
                'key2:1:2' => [
                  'contains' => [
                    'key2:1:2:1' => [
                      'default' => 'value2:1:2:1',
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
      'expected' => [
        'key2' => [
          'key2:1' => [
            'key2:1:1' => 'value0',
            'key2:1:2' => [
              'key2:1:2:1' => 'value1',
            ],
          ],
        ],
      ],
    ];
    return $test_parameters;
  }
  
  /**
   * Data provider for testSetOptionDefault().
   *
   * @return array
   *   An array of test data.
   */
  public static function providerTestSetOptionDefault() {
    $test_parameters = [];
    // No definition should change anything on the storage.
    $test_parameters[] = [
      'storage' => [],
      'definition' => [],
      'expected' => [],
    ];
    // Set a single definition, which should be picked up.
    $test_parameters[] = [
      'storage' => [],
      'definition' => [
        'key' => [
          'default' => 'value',
        ],
      ],
      'expected' => [
        'key' => 'value',
      ],
    ];
    // Set multiple keys, all should be picked up.
    $test_parameters[] = [
      'storage' => [],
      'definition' => [
        'key' => [
          'default' => 'value',
        ],
        'key2' => [
          'default' => 'value2',
        ],
        'key3' => [
          'default' => 'value3',
        ],
      ],
      'expected' => [
        'key' => 'value',
        'key2' => 'value2',
        'key3' => 'value3',
      ],
    ];
    // Setup a definition with multiple levels.
    $test_parameters[] = [
      'storage' => [],
      'definition' => [
        'key' => [
          'default' => 'value',
        ],
        'key2' => [
          'contains' => [
            'key2:1' => [
              'default' => 'value2:1',
            ],
            'key2:2' => [
              'default' => 'value2:2',
            ],
          ],
        ],
      ],
      'expected' => [
        'key' => 'value',
        'key2' => [
          'key2:1' => 'value2:1',
          'key2:2' => 'value2:2',
        ],
      ],
    ];
    return $test_parameters;
  }
  
  /**
   * @covers ::filterByDefinedOptions
   * @dataProvider providerTestFilterByDefinedOptions
   */
  public function testFilterByDefinedOptions($storage, $options, $expected_storage) : void {
    $this->testHelperPlugin
      ->setDefinedOptions($options);
    $this->testHelperPlugin
      ->filterByDefinedOptions($storage);
    $this->assertEquals($expected_storage, $storage);
  }
  
  /**
   * Provides data to testFilterByDefinedOptions().
   */
  public static function providerTestFilterByDefinedOptions() {
    $data = [];
    // A simple defined option.
    $values_1 = [
      'key1' => 'value1',
    ];
    $options_1 = [
      'key1' => [
        'default' => '',
      ],
    ];
    $data[] = [
      $values_1,
      $options_1,
      $values_1,
    ];
    // Multiple defined options .
    $values_2 = [
      'key1' => 'value1',
      'key2' => 'value2',
    ];
    $options_2 = [
      'key1' => [
        'default' => '',
      ],
      'key2' => [
        'default' => '',
      ],
    ];
    $data[] = [
      $values_2,
      $options_2,
      $values_2,
    ];
    // Multiple options, just one defined.
    $data[] = [
      $values_2,
      $options_1,
      $values_1,
    ];
    // Nested options, all properly defined.
    $data[] = [
      [
        'sub1' => $values_2,
        'sub2' => $values_2,
      ],
      [
        'sub1' => [
          'contains' => $options_2,
        ],
        'sub2' => [
          'contains' => $options_2,
        ],
      ],
      [
        'sub1' => $values_2,
        'sub2' => $values_2,
      ],
    ];
    // Nested options, not all properly defined.
    $data[] = [
      [
        'sub1' => $values_2,
        'sub2' => $values_2,
      ],
      [
        'sub1' => [
          'contains' => $options_2,
        ],
        'sub2' => [
          'contains' => $options_1,
        ],
      ],
      [
        'sub1' => $values_2,
        'sub2' => $values_1,
      ],
    ];
    return $data;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
PluginBaseTest::$testHelperPlugin protected property The test helper plugin to use for the tests.
PluginBaseTest::providerTestFilterByDefinedOptions public static function Provides data to testFilterByDefinedOptions().
PluginBaseTest::providerTestSetOptionDefault public static function Data provider for testSetOptionDefault().
PluginBaseTest::providerTestUnpackOptions public static function Data provider for testUnpackOptions().
PluginBaseTest::setUp protected function Overrides UnitTestCase::setUp
PluginBaseTest::testFilterByDefinedOptions public function @covers ::filterByDefinedOptions[[api-linebreak]]
@dataProvider providerTestFilterByDefinedOptions
PluginBaseTest::testSetOptionDefault public function Tests the setOptionDefault method.
PluginBaseTest::testUnpackOptions public function Tests the unpackOptions method.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.