You are here

class ExtraFieldFormManagerTest in Extra Field 8.2

@coversDefaultClass \Drupal\extra_field\Plugin\ExtraFieldFormManager

@group extra_field

Hierarchy

Expanded class hierarchy of ExtraFieldFormManagerTest

File

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

Namespace

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

  /**
   * The plugin manager under test.
   *
   * @var \Drupal\extra_field\Plugin\ExtraFieldFormManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $formManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->formManager = $this
      ->getMockBuilder('Drupal\\extra_field\\Plugin\\ExtraFieldFormManager')
      ->disableOriginalConstructor()
      ->setMethods([
      'getDefinitions',
      'allEntityBundles',
    ])
      ->getMock();
  }

  /**
   * Prepare ::getDefinitions to return the right values.
   *
   * @param array $definitions
   *   The plugin definitions to return.
   */
  protected function prepareDefinitions(array $definitions) {
    $this->formManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->will($this
      ->returnValue($definitions));
  }

  /**
   * Prepare ::allEntityBundles to return the right values.
   *
   * @param array $bundlesMap
   *   Array of bundle names.
   */
  protected function prepareEntityBundles(array $bundlesMap) {
    $this->formManager
      ->expects($this
      ->any())
      ->method('allEntityBundles')
      ->will($this
      ->returnValueMap($bundlesMap));
  }

  /**
   * @covers ::fieldInfo
   *
   * @dataProvider fieldInfoProvider
   *
   * @param array $definitions
   *   Plugin definitions as returned by ::getDefinitions.
   * @param array $bundles
   *   Entity bundles as returned by ::allEntityBundles.
   * @param array $results
   *   Field info as returned by ::fieldInfo.
   */
  public function testFieldInfo(array $definitions, array $bundles, array $results) {
    $this
      ->prepareDefinitions($definitions);
    $this
      ->prepareEntityBundles($bundles);
    $this
      ->assertEquals(count($this->formManager
      ->getDefinitions()), count($definitions));
    $this
      ->assertEquals($this->formManager
      ->fieldInfo(), $results);
  }

  /**
   * Data provider for testFieldInfo().
   */
  public function fieldInfoProvider() {
    $info[] = [
      // Definitions.
      [
        'test' => [
          'id' => 'test',
          'bundles' => [
            'node.article',
          ],
          'label' => 'test form node article',
          'description' => 'test description form node article',
          'weight' => 0,
          'visible' => FALSE,
        ],
      ],
      // Bundles.
      [],
      // Results.
      [
        'node' => [
          'article' => [
            'form' => [
              'extra_field_test' => [
                'label' => 'test form node article',
                'description' => 'test description form node article',
                'weight' => 0,
                'visible' => FALSE,
              ],
            ],
          ],
        ],
      ],
    ];
    $info[] = [
      // Definitions.
      [
        'test' => [
          'id' => 'test',
          'bundles' => [
            'node.article',
          ],
          'label' => 'test form node article',
          'description' => 'test description form node article',
          'weight' => 88,
          'visible' => TRUE,
        ],
      ],
      // Bundles.
      [],
      // Results.
      [
        'node' => [
          'article' => [
            'form' => [
              'extra_field_test' => [
                'label' => 'test form node article',
                'description' => 'test description form node article',
                'weight' => 88,
                'visible' => TRUE,
              ],
            ],
          ],
        ],
      ],
    ];
    $info[] = [
      // Definitions.
      [
        'test1' => [
          'id' => 'test1',
          'bundles' => [
            'node.*',
            'come.*',
          ],
          'label' => 'test form 1',
          'description' => 'test description form 1',
          'weight' => 0,
          'visible' => FALSE,
        ],
        'test2' => [
          'id' => 'test2',
          'bundles' => [
            'node.article',
          ],
          'label' => 'test form 2',
          'description' => 'test description form 2',
          'weight' => 2,
          'visible' => TRUE,
        ],
      ],
      // Bundles.
      [
        [
          'node',
          [
            'article',
            'story',
            'blog',
          ],
        ],
        [
          'come',
          [
            'rain',
            'shine',
          ],
        ],
      ],
      // Results.
      [
        'node' => [
          'article' => [
            'form' => [
              'extra_field_test1' => [
                'label' => 'test form 1',
                'description' => 'test description form 1',
                'weight' => 0,
                'visible' => FALSE,
              ],
              'extra_field_test2' => [
                'label' => 'test form 2',
                'description' => 'test description form 2',
                'weight' => 2,
                'visible' => TRUE,
              ],
            ],
          ],
          'story' => [
            'form' => [
              'extra_field_test1' => [
                'label' => 'test form 1',
                'description' => 'test description form 1',
                'weight' => 0,
                'visible' => FALSE,
              ],
            ],
          ],
          'blog' => [
            'form' => [
              'extra_field_test1' => [
                'label' => 'test form 1',
                'description' => 'test description form 1',
                'weight' => 0,
                'visible' => FALSE,
              ],
            ],
          ],
        ],
        'come' => [
          'rain' => [
            'form' => [
              'extra_field_test1' => [
                'label' => 'test form 1',
                'description' => 'test description form 1',
                'weight' => 0,
                'visible' => FALSE,
              ],
            ],
          ],
          'shine' => [
            'form' => [
              'extra_field_test1' => [
                'label' => 'test form 1',
                'description' => 'test description form 1',
                'weight' => 0,
                'visible' => FALSE,
              ],
            ],
          ],
        ],
      ],
    ];
    return $info;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExtraFieldFormManagerTest::$formManager protected property The plugin manager under test.
ExtraFieldFormManagerTest::fieldInfoProvider public function Data provider for testFieldInfo().
ExtraFieldFormManagerTest::prepareDefinitions protected function Prepare ::getDefinitions to return the right values.
ExtraFieldFormManagerTest::prepareEntityBundles protected function Prepare ::allEntityBundles to return the right values.
ExtraFieldFormManagerTest::setUp protected function Overrides UnitTestCase::setUp
ExtraFieldFormManagerTest::testFieldInfo public function @covers ::fieldInfo
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.