You are here

class ExtraFieldManagerBaseTest in Extra Field 8.2

@coversDefaultClass \Drupal\extra_field\Plugin\ExtraFieldManagerBase

@group extra_field

Hierarchy

Expanded class hierarchy of ExtraFieldManagerBaseTest

File

tests/src/Unit/ExtraFieldManagerBaseTest.php, line 12

Namespace

Drupal\Tests\extra_field\Unit
View source
class ExtraFieldManagerBaseTest extends UnitTestCase {

  /**
   * The ExtraFieldManagerBase under test.
   *
   * @var \Drupal\extra_field\Plugin\ExtraFieldManagerBaseInterface
   */
  protected $baseManager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->baseManager = $this
      ->getMockBuilder('Drupal\\extra_field\\Plugin\\ExtraFieldManagerBase')
      ->disableOriginalConstructor()
      ->setMethods([
      'getEntityTypeManager',
      'getEntityBundleType',
      'getEntityBundles',
    ])
      ->getMockForAbstractClass();
  }

  /**
   * @covers ::matchEntityBundleKey
   *
   * @dataProvider matchEntityBundleKeyProvider
   *
   * @param array $pluginBundles
   *   Array of entity-bundle pairs the plugin can be used for.
   * @param string $entityBundleKey
   *   The entity-bundle string of a content entity to check.
   * @param bool $match
   *   The match result.
   */
  public function testMatchEntityBundleKey(array $pluginBundles, $entityBundleKey, $match) {
    $result = self::callMethod($this->baseManager, 'matchEntityBundleKey', [
      $pluginBundles,
      $entityBundleKey,
    ]);
    $this
      ->assertEquals($result, $match);
  }

  /**
   * Data provider for matchEntityBundleKey.
   *
   * @return array
   *   Contains:
   *   - pluginBundles (array)
   *   - entityBundleKey (string)
   *   - output as returned by ::matchEntityBundleKey.
   */
  public function matchEntityBundleKeyProvider() {
    return [
      [
        // pluginBundles.
        [],
        // entityBundleKey.
        '',
        // result.
        FALSE,
      ],
      [
        [
          'entity_type.bundle',
        ],
        '',
        FALSE,
      ],
      [
        [
          '',
        ],
        'entity_type.bundle',
        FALSE,
      ],
      [
        [
          'entity_type.bundle',
        ],
        'entity_type.bundle',
        TRUE,
      ],
      [
        [
          'entity_type.*',
        ],
        'entity_type.bundle',
        TRUE,
      ],
      [
        [
          'entity_type.bundle',
        ],
        'entity_type.other_bundle',
        FALSE,
      ],
      [
        [
          'entity_type.bundle',
          'entity_type.*',
        ],
        'entity_type.other',
        TRUE,
      ],
      [
        [
          'other_entity_type.bundle',
        ],
        'entity_type.bundle',
        FALSE,
      ],
      [
        [
          'other_entity_type.bundle',
          'other_entity_type.*',
        ],
        'entity_type.bundle',
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::supportedEntityBundles
   *
   * @dataProvider supportedEntityBundlesProvider
   */
  public function testSupportedEntityBundles($entityBundleKeys, $result) {
    $this->baseManager
      ->expects($this
      ->any())
      ->method('getEntityBundleType')
      ->will($this
      ->returnValueMap([
      [
        'without_bundles',
        NULL,
      ],
      [
        'node',
        'node_type',
      ],
    ]));
    $this->baseManager
      ->expects($this
      ->any())
      ->method('getEntityBundles')
      ->will($this
      ->returnValueMap([
      [
        'node_type',
        [
          'article' => 'article',
          'page' => 'page',
        ],
      ],
    ]));
    $supportedEntityBundles = self::callMethod($this->baseManager, 'supportedEntityBundles', [
      $entityBundleKeys,
    ]);
    $this
      ->assertEquals($result, $supportedEntityBundles);
  }

  /**
   * Data provider for supportedEntityBundles.
   *
   * @return array
   *   Contains:
   *   - Entity bundle keys.
   *   - Output as retured by ::supportedEntityBundles
   */
  public function supportedEntityBundlesProvider() {
    return [
      [
        // Entity bundle keys.
        [
          '',
        ],
        // Result.
        [],
      ],
      [
        // Entity bundle keys.
        [
          '*',
        ],
        // Result.
        [],
      ],
      [
        // Entity bundle keys.
        [
          'node.article',
        ],
        // Result.
        [
          'node.article' => [
            'entity' => 'node',
            'bundle' => 'article',
          ],
        ],
      ],
      [
        // Entity bundle keys.
        [
          'node.*',
        ],
        // Result.
        [
          'node.article' => [
            'entity' => 'node',
            'bundle' => 'article',
          ],
          'node.page' => [
            'entity' => 'node',
            'bundle' => 'page',
          ],
        ],
      ],
      [
        // Entity bundle keys.
        [
          'no_bundles.*',
        ],
        // Result.
        [
          'no_bundles.no_bundles' => [
            'entity' => 'no_bundles',
            'bundle' => 'no_bundles',
          ],
        ],
      ],
    ];
  }

  /**
   * @covers ::allEntityBundles
   *
   * @dataProvider allEntityBundlesProvider
   *
   * @param string $entityType
   *   The entity type for which to get the bundles.
   * @param string $bundleType
   *   The entity bundle type.
   * @param array $entityBundles
   *   Structured array of entity bundles. Keys and values are equal.
   * @param array $result
   *   The entity bundles as returned by ::allEntityBundles.
   */
  public function testAllEntityBundles($entityType, $bundleType, array $entityBundles, array $result) {
    $this->baseManager
      ->expects($this
      ->any())
      ->method('getEntityBundleType')
      ->will($this
      ->returnValue($bundleType));
    $this->baseManager
      ->expects($this
      ->any())
      ->method('getEntityBundles')
      ->will($this
      ->returnValue($entityBundles));
    $allEntityBundles = self::callMethod($this->baseManager, 'allEntityBundles', [
      $entityType,
    ]);
    $this
      ->assertEquals($result, $allEntityBundles);
  }

  /**
   * Data provider for allEntityBundles.
   *
   * @return array
   *   Contains:
   *   - entityType (string)
   *   - bundleType (string)
   *   - entityBundles (array)
   *   - output as returned by ::allEntityBundles.
   */
  public function allEntityBundlesProvider() {
    return [
      [
        // Entity type.
        'entity_type_without_bundle',
        // Bundle type.
        NULL,
        // Entity bundles.
        [],
        // Result.
        [
          'entity_type_without_bundle' => 'entity_type_without_bundle',
        ],
      ],
      [
        // Entity type.
        'node',
        // Bundle type.
        'node_type',
        // Entity bundles.
        [
          'article' => 'article',
          'page' => 'page',
        ],
        // Result.
        [
          'article' => 'article',
          'page' => 'page',
        ],
      ],
    ];
  }

  /**
   * @covers ::entityBundleKey
   */
  public function testFieldName() {
    $result = self::callMethod($this->baseManager, 'fieldName', [
      'plugin_id',
    ]);
    $this
      ->assertEquals('extra_field_plugin_id', $result);
  }

  /**
   * @covers ::entityBundleKey
   */
  public function testEntityBundleKey() {
    $result = self::callMethod($this->baseManager, 'entityBundleKey', [
      'foo',
      'bar',
    ]);
    $this
      ->assertEquals('foo.bar', $result);
  }

  /**
   * Calls protected methods on an abstract class.
   *
   * @param object $object
   *   The class to call the method on.
   * @param string $name
   *   The method name.
   * @param array $arguments
   *   The arguments to call the method with.
   *
   * @return mixed
   *   The method result.
   */
  protected static function callMethod($object, $name, array $arguments) {
    $class = new \ReflectionClass($object);
    $method = $class
      ->getMethod($name);
    $method
      ->setAccessible(TRUE);
    return $method
      ->invokeArgs($object, $arguments);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExtraFieldManagerBaseTest::$baseManager protected property The ExtraFieldManagerBase under test.
ExtraFieldManagerBaseTest::allEntityBundlesProvider public function Data provider for allEntityBundles.
ExtraFieldManagerBaseTest::callMethod protected static function Calls protected methods on an abstract class.
ExtraFieldManagerBaseTest::matchEntityBundleKeyProvider public function Data provider for matchEntityBundleKey.
ExtraFieldManagerBaseTest::setUp public function Overrides UnitTestCase::setUp
ExtraFieldManagerBaseTest::supportedEntityBundlesProvider public function Data provider for supportedEntityBundles.
ExtraFieldManagerBaseTest::testAllEntityBundles public function @covers ::allEntityBundles
ExtraFieldManagerBaseTest::testEntityBundleKey public function @covers ::entityBundleKey
ExtraFieldManagerBaseTest::testFieldName public function @covers ::entityBundleKey
ExtraFieldManagerBaseTest::testMatchEntityBundleKey public function @covers ::matchEntityBundleKey
ExtraFieldManagerBaseTest::testSupportedEntityBundles public function @covers ::supportedEntityBundles
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.