You are here

class ViewPaymentsByOwnerTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Plugin\views\argument_validator\ViewPaymentsByOwner

@group Payment

Hierarchy

Expanded class hierarchy of ViewPaymentsByOwnerTest

File

tests/src/Unit/Plugin/views/argument_validator/ViewPaymentsByOwnerTest.php, line 20

Namespace

Drupal\Tests\payment\Unit\Plugin\views\argument_validator
View source
class ViewPaymentsByOwnerTest extends UnitTestCase {

  /**
   * The current user
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $currentUser;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;

  /**
   * The plugin definition.
   *
   * @var mixed[]
   */
  protected $pluginDefinition = [];

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\views\argument_validator\ViewPaymentsByOwner
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->currentUser = $this
      ->createMock(AccountInterface::class);
    $entity_type_bundle_info = $this
      ->createMock(EntityTypeBundleInfoInterface::class);
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition = [
      'entity_type' => $this
        ->randomMachineName(),
    ];
    $this->sut = new ViewPaymentsByOwner($configuration, $plugin_id, $this->pluginDefinition, $this->entityTypeManager, $entity_type_bundle_info);
    $this->sut
      ->setCurrentUser($this->currentUser);
    $options = [
      'access' => FALSE,
      'bundles' => [],
      'multiple' => TRUE,
      'operation' => NULL,
    ];
    $view_executable = $this
      ->getMockBuilder(ViewExecutable::class)
      ->disableOriginalConstructor()
      ->getMock();
    $display = $this
      ->getMockBuilder(DisplayPluginBase::class)
      ->disableOriginalConstructor()
      ->getMockForAbstractClass();
    $this->sut
      ->init($view_executable, $display, $options);
  }

  /**
   * @covers ::validateArgument
   */
  public function xtestValidateArgumentWithoutValidEntities() {
    $entity_storage = $this
      ->createMock(EntityStorageInterface::class);
    $entity_storage
      ->expects($this
      ->atLeastOnce())
      ->method('loadMultiple')
      ->willReturn([]);
    $this->entityTypeManager
      ->expects($this
      ->atLeastOnce())
      ->method('getStorage')
      ->with($this->pluginDefinition['entity_type'])
      ->willReturn($entity_storage);
    $argument = mt_rand();
    $this
      ->assertFalse($this->sut
      ->validateArgument($argument));
  }

  /**
   * @covers ::validateArgument
   *
   * @dataProvider providerValidateArgument
   */
  public function testValidateArgument($expected_validity, $argument, $current_user_id, array $permissions) {
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity_storage = $this
      ->createMock(EntityStorageInterface::class);
    $entity_storage
      ->expects($this
      ->atLeastOnce())
      ->method('loadMultiple')
      ->willReturn([
      7 => $entity,
      9 => $entity,
    ]);
    $this->entityTypeManager
      ->expects($this
      ->atLeastOnce())
      ->method('getStorage')
      ->with($this->pluginDefinition['entity_type'])
      ->willReturn($entity_storage);
    $this->currentUser
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($current_user_id);
    $map = [];
    foreach ($permissions as $permission) {
      $map[] = [
        $permission,
        TRUE,
      ];
    }
    $this->currentUser
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap($map);
    $this
      ->assertSame($expected_validity, $this->sut
      ->validateArgument($argument));
  }

  /**
   * Provides data to self::testValidateArgument().
   */
  public function providerValidateArgument() {
    return [
      // Permissions to view own paymens only.
      [
        TRUE,
        '7',
        7,
        [
          'payment.payment.view.own',
        ],
      ],
      [
        FALSE,
        '7+9',
        7,
        [
          'payment.payment.view.own',
        ],
      ],
      [
        FALSE,
        '7,9',
        7,
        [
          'payment.payment.view.own',
        ],
      ],
      [
        FALSE,
        '9',
        7,
        [
          'payment.payment.view.own',
        ],
      ],
      // Permissions to view any payment.
      [
        TRUE,
        '7',
        7,
        [
          'payment.payment.view.any',
        ],
      ],
      [
        TRUE,
        '7+9',
        7,
        [
          'payment.payment.view.any',
        ],
      ],
      [
        TRUE,
        '7,9',
        7,
        [
          'payment.payment.view.any',
        ],
      ],
      // Permissions to view own and any payments.
      [
        TRUE,
        '7',
        7,
        [
          'payment.payment.view.any',
          'payment.payment.view.own',
        ],
      ],
      [
        TRUE,
        '7+9',
        7,
        [
          'payment.payment.view.any',
          'payment.payment.view.own',
        ],
      ],
      [
        TRUE,
        '7,9',
        7,
        [
          'payment.payment.view.any',
          'payment.payment.view.own',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ViewPaymentsByOwnerTest::$currentUser protected property The current user
ViewPaymentsByOwnerTest::$entityTypeManager protected property The entity type manager.
ViewPaymentsByOwnerTest::$pluginDefinition protected property The plugin definition.
ViewPaymentsByOwnerTest::$sut protected property The class under test.
ViewPaymentsByOwnerTest::providerValidateArgument public function Provides data to self::testValidateArgument().
ViewPaymentsByOwnerTest::setUp public function Overrides UnitTestCase::setUp
ViewPaymentsByOwnerTest::testValidateArgument public function @covers ::validateArgument
ViewPaymentsByOwnerTest::xtestValidateArgumentWithoutValidEntities public function @covers ::validateArgument