You are here

class SortableviewsAccessTest in Sortableviews 8

@coversDefaultClass \Drupal\sortableviews\Access\SortableviewsAccess @group sortableviews

Hierarchy

Expanded class hierarchy of SortableviewsAccessTest

File

tests/src/Unit/Access/SortableviewsAccessTest.php, line 15

Namespace

Drupal\Tests\sortableviews\Unit\Access
View source
class SortableviewsAccessTest extends UnitTestCase {

  /**
   * The instance of SortableviewsAccess to be tested.
   *
   * @var \Drupal\sortableviews\Access\SortableviewsAccess
   */
  protected $sortableViewsAccess;

  /**
   * An object mocked from AccountInterface.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $view_style = $this
      ->getMockBuilder('Drupal\\views\\Plugin\\views\\style\\StylePluginBase')
      ->disableOriginalConstructor()
      ->getMock();
    $view_style->options['weight_field'] = 'some_field';
    $entity_type = $this
      ->createMock('Drupal\\Component\\Plugin\\Definition\\PluginDefinitionInterface');
    $entity_type
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('some_entity');
    $view_executable = $this
      ->getMockBuilder('Drupal\\views\\ViewExecutable')
      ->disableOriginalConstructor()
      ->getMock();
    $view_executable
      ->expects($this
      ->any())
      ->method('getBaseEntityType')
      ->willReturn($entity_type);
    $view_executable
      ->expects($this
      ->any())
      ->method('getStyle')
      ->willReturn($view_style);
    $view_entity = $this
      ->createMock('Drupal\\views\\ViewEntityInterface');
    $executable_factory = $this
      ->getMockBuilder('Drupal\\views\\ViewExecutableFactory')
      ->disableOriginalConstructor()
      ->getMock();
    $executable_factory
      ->expects($this
      ->any())
      ->method('get')
      ->with($view_entity)
      ->willReturn($view_executable);
    $entities = [];
    for ($aux = 1; $aux <= 2; $aux++) {
      $entity = $this
        ->createMock('Drupal\\Core\\Access\\AccessibleInterface');
      $entity
        ->expects($this
        ->any())
        ->method('access')
        ->willReturn(TRUE);
      $entities[] = $entity;
    }
    $entity_storage = $this
      ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $entity_storage
      ->expects($this
      ->any())
      ->method('load')
      ->with('some_view')
      ->willReturn($view_entity);
    $entity_storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturn($entities);
    $entity_manager = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $entity_manager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->willReturn($entity_storage);
    $this->sortableViewsAccess = new SortableviewsAccess($entity_manager, $executable_factory);
    $this->account = $this
      ->createMock('Drupal\\Core\\Session\\AccountInterface');
  }

  /**
   * Tests the access method.
   *
   * @covers ::access
   *
   * @dataProvider dataProvider
   */
  public function testAccess(Request $request, $is_valid) {
    $result = $this->sortableViewsAccess
      ->access($request, $this->account);
    if ($is_valid) {
      $this
        ->assertTrue($result instanceof AccessResultAllowed);
      $this
        ->assertEquals($request
        ->get('entity_type'), 'some_entity');
      $this
        ->assertEquals($request
        ->get('weight_field'), 'some_field');
    }
    else {
      $this
        ->assertTrue($result instanceof AccessResultForbidden);
    }
  }

  /**
   * Provides test data for testAccess().
   */
  public function dataProvider() {
    $data = [];
    $request = new Request();
    $data[] = [
      $request,
      FALSE,
    ];
    unset($request);
    $request = new Request();
    $request->attributes
      ->set('view_name', 'some_view');
    $request->attributes
      ->set('display_name', uniqid());
    $request->attributes
      ->set('current_order', [
      1,
      2,
    ]);
    $data[] = [
      $request,
      TRUE,
    ];
    unset($request);
    return $data;
  }

}

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.
SortableviewsAccessTest::$account protected property An object mocked from AccountInterface.
SortableviewsAccessTest::$sortableViewsAccess protected property The instance of SortableviewsAccess to be tested.
SortableviewsAccessTest::dataProvider public function Provides test data for testAccess().
SortableviewsAccessTest::setUp protected function Overrides UnitTestCase::setUp
SortableviewsAccessTest::testAccess public function Tests the access method.
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.