You are here

class AgreementEntityTest in Agreement 8.2

Same name and namespace in other branches
  1. 3.0.x tests/src/Unit/Plugin/views/field/AgreementEntityTest.php \Drupal\Tests\agreement\Unit\Plugin\views\field\AgreementEntityTest

Tests the agreement entity views field plugin.

@group agreement

Hierarchy

Expanded class hierarchy of AgreementEntityTest

File

tests/src/Unit/Plugin/views/field/AgreementEntityTest.php, line 16

Namespace

Drupal\Tests\agreement\Unit\Plugin\views\field
View source
class AgreementEntityTest extends UnitTestCase {

  /**
   * Agreement entity plugin.
   *
   * @var \Drupal\agreement\Plugin\views\field\AgreementEntity
   */
  protected $plugin;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $agreement = new Agreement([
      'id' => 'default',
      'label' => 'Default agreement',
      'path' => '/agreement',
      'agreement' => '',
      'settings' => [
        'visibility' => [
          'settings' => 0,
          'pages' => [],
        ],
        'roles' => [
          'authenticated',
        ],
        'frequency' => -1,
        'title' => 'Our agreement',
      ],
    ], 'agreement');
    $styleProphet = $this
      ->prophesize('\\Drupal\\views\\Plugin\\views\\style\\DefaultStyle');
    $viewProphet = $this
      ->prophesize('\\Drupal\\views\\ViewExecutable');
    $viewProphet
      ->getStyle()
      ->willReturn($styleProphet
      ->reveal());
    $storageProphet = $this
      ->prophesize('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
    $storageProphet
      ->loadMultiple()
      ->willReturn([
      'default' => $agreement,
    ]);
    $entityManagerProphet = $this
      ->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $entityManagerProphet
      ->getStorage('agreement')
      ->willReturn($storageProphet
      ->reveal());
    $definition = [
      'id' => 'agreement_entity',
    ];
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entityManagerProphet
      ->reveal());
    $container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    \Drupal::setContainer($container);
    $this->plugin = new AgreementEntity([], 'agreement_entity', $definition, $container
      ->get('entity_type.manager'));
    $this->plugin->view = $viewProphet
      ->reveal();
    $this->plugin->field_alias = 'type';
  }

  /**
   * Asserts the default display option.
   */
  public function testDefineOptions() {
    $expected = [
      'default' => [
        'label',
      ],
    ];
    $this
      ->assertEquals($expected, $this->plugin
      ->defineOptions()['display']);
  }

  /**
   * Asserts agreement entity set on results.
   */
  public function testPreRender() {
    $values = [
      new ResultRow([
        'type' => 'default',
        'uid' => 1,
      ]),
      new ResultRow([
        'type' => 'default',
        'uid' => 2,
      ]),
      new ResultRow([
        'uid' => 3,
      ]),
    ];
    $this->plugin
      ->preRender($values);
    $this
      ->assertObjectHasAttribute('_agreement', $values[0]);
  }

  /**
   * Asserts that render builds markup based on options.
   *
   * @param array $options
   *   An array of "display" options.
   * @param string $expected_key
   *   The expected array key or result.
   *
   * @dataProvider renderProvider
   */
  public function testRender(array $options, $expected_key) {
    $this->plugin->options += [
      'display' => $options,
    ];
    $values = [
      new ResultRow([
        'type' => 'default',
        'uid' => 2,
      ]),
    ];
    $this->plugin
      ->preRender($values);
    $markup = $this->plugin
      ->render($values[0]);
    if (empty($options)) {
      $this
        ->assertEquals($expected_key, $markup);
    }
    else {
      $this
        ->assertArrayHasKey($expected_key, $markup);
    }
  }

  /**
   * Provides test arguments for testing render method.
   *
   * @return array
   *   An indexed array of tests to run with test arguments.
   */
  public function renderProvider() {
    return [
      [
        [],
        'default',
      ],
      [
        [
          'id',
        ],
        'id',
      ],
      [
        [
          'label',
        ],
        'label',
      ],
      [
        [
          'path',
        ],
        'path',
      ],
      [
        [
          'roles',
        ],
        'roles',
      ],
      [
        [
          'title',
        ],
        'title',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AgreementEntityTest::$plugin protected property Agreement entity plugin.
AgreementEntityTest::renderProvider public function Provides test arguments for testing render method.
AgreementEntityTest::setUp protected function Overrides UnitTestCase::setUp
AgreementEntityTest::testDefineOptions public function Asserts the default display option.
AgreementEntityTest::testPreRender public function Asserts agreement entity set on results.
AgreementEntityTest::testRender public function Asserts that render builds markup based on options.
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.