You are here

class EntityTest in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/hal/src/Tests/EntityTest.php \Drupal\hal\Tests\EntityTest
  2. 8 core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  3. 8 core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest
  4. 8 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest
Same name and namespace in other branches
  1. 8.0 core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest

@coversDefaultClass \Drupal\views\Plugin\views\argument_validator\Entity @group views

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
    • class \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest

Expanded class hierarchy of EntityTest

File

core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php, line 17
Contains \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest.

Namespace

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

  /**
   * The view executable.
   *
   * @var \Drupal\views\ViewExecutable
   */
  protected $executable;

  /**
   * The view display.
   *
   * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
   */
  protected $display;

  /**
   * The entity manager.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The tested argument validator.
   *
   * @var \Drupal\views\Plugin\views\argument_validator\Entity
   */
  protected $argumentValidator;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->entityManager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
    $mock_entity = $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(), '', FALSE, TRUE, TRUE, array(
      'bundle',
      'access',
    ));
    $mock_entity
      ->expects($this
      ->any())
      ->method('bundle')
      ->will($this
      ->returnValue('test_bundle'));
    $mock_entity
      ->expects($this
      ->any())
      ->method('access')
      ->will($this
      ->returnValueMap(array(
      array(
        'test_op',
        NULL,
        FALSE,
        TRUE,
      ),
      array(
        'test_op_2',
        NULL,
        FALSE,
        FALSE,
      ),
      array(
        'test_op_3',
        NULL,
        FALSE,
        TRUE,
      ),
    )));
    $mock_entity_bundle_2 = $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(), '', FALSE, TRUE, TRUE, array(
      'bundle',
      'access',
    ));
    $mock_entity_bundle_2
      ->expects($this
      ->any())
      ->method('bundle')
      ->will($this
      ->returnValue('test_bundle_2'));
    $mock_entity_bundle_2
      ->expects($this
      ->any())
      ->method('access')
      ->will($this
      ->returnValueMap(array(
      array(
        'test_op',
        NULL,
        FALSE,
        FALSE,
      ),
      array(
        'test_op_2',
        NULL,
        FALSE,
        FALSE,
      ),
      array(
        'test_op_3',
        NULL,
        FALSE,
        TRUE,
      ),
    )));
    $storage = $this
      ->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');

    // Setup values for IDs passed as strings or numbers.
    $value_map = array(
      array(
        array(),
        array(),
      ),
      array(
        array(
          1,
        ),
        array(
          1 => $mock_entity,
        ),
      ),
      array(
        array(
          '1',
        ),
        array(
          1 => $mock_entity,
        ),
      ),
      array(
        array(
          1,
          2,
        ),
        array(
          1 => $mock_entity,
          2 => $mock_entity_bundle_2,
        ),
      ),
      array(
        array(
          '1',
          '2',
        ),
        array(
          1 => $mock_entity,
          2 => $mock_entity_bundle_2,
        ),
      ),
      array(
        array(
          2,
        ),
        array(
          2 => $mock_entity_bundle_2,
        ),
      ),
      array(
        array(
          '2',
        ),
        array(
          2 => $mock_entity_bundle_2,
        ),
      ),
    );
    $storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->will($this
      ->returnValueMap($value_map));
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('entity_test')
      ->will($this
      ->returnValue($storage));
    $this->executable = $this
      ->getMockBuilder('Drupal\\views\\ViewExecutable')
      ->disableOriginalConstructor()
      ->getMock();
    $this->display = $this
      ->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
      ->disableOriginalConstructor()
      ->getMock();
    $definition = array(
      'entity_type' => 'entity_test',
    );
    $this->argumentValidator = new Entity(array(), 'entity_test', $definition, $this->entityManager);
  }

  /**
   * Tests the validate argument method with no access and bundles.
   *
   * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
   */
  public function testValidateArgumentNoAccess() {
    $options = array();
    $options['access'] = FALSE;
    $options['bundles'] = array();
    $this->argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(3));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(''));
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument(1));
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument(2));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument('1,2'));
  }

  /**
   * Tests the validate argument method with access and no bundles.
   *
   * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
   */
  public function testValidateArgumentAccess() {
    $options = array();
    $options['access'] = TRUE;
    $options['bundles'] = array();
    $options['operation'] = 'test_op';
    $this->argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(3));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(''));
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument(1));
    $options = array();
    $options['access'] = TRUE;
    $options['bundles'] = array();
    $options['operation'] = 'test_op_2';
    $this->argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(3));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(''));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(1));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(2));
  }

  /**
   * Tests the validate argument method with bundle checking.
   */
  public function testValidateArgumentBundle() {
    $options = array();
    $options['access'] = FALSE;
    $options['bundles'] = array(
      'test_bundle' => 1,
    );
    $this->argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument(1));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument(2));
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {

    // Create an entity manager, storage, entity type, and entity to mock the
    // loading of entities providing bundles.
    $entityManager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
    $storage = $this
      ->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $entity_type = $this
      ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $mock_entity = $this
      ->getMock('Drupal\\Core\\Entity\\EntityInterface');
    $mock_entity
      ->expects($this
      ->any())
      ->method('getConfigDependencyKey')
      ->willReturn('config');
    $mock_entity
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn('test_bundle');
    $storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->with([
      'test_bundle',
    ])
      ->willReturn([
      'test_bundle' => $mock_entity,
    ]);
    $entity_type
      ->expects($this
      ->any())
      ->method('getBundleEntityType')
      ->willReturn('entity_test_bundle');
    $entityManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->with('entity_test')
      ->willReturn($entity_type);
    $entityManager
      ->expects($this
      ->any())
      ->method('hasHandler')
      ->with('entity_test_bundle', 'storage')
      ->willReturn(TRUE);
    $entityManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('entity_test_bundle')
      ->willReturn($storage);

    // Set up the argument validator.
    $argumentValidator = new Entity(array(), 'entity_test', [
      'entity_type' => 'entity_test',
    ], $entityManager);
    $options = array();
    $options['access'] = FALSE;
    $options['bundles'] = array(
      'test_bundle' => 1,
    );
    $argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertEquals([
      'config' => [
        'test_bundle',
      ],
    ], $argumentValidator
      ->calculateDependencies());
  }

  /**
   * Tests the validate argument method with multiple argument splitting.
   */
  public function testValidateArgumentMultiple() {
    $options = array();
    $options['access'] = TRUE;
    $options['bundles'] = array();
    $options['operation'] = 'test_op';
    $options['multiple'] = TRUE;
    $this->argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument('1'));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument('2'));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument('1,2'));
    $this
      ->assertFalse($this->argumentValidator
      ->validateArgument('1+2'));
    $options = array();
    $options['access'] = TRUE;
    $options['bundles'] = array();
    $options['operation'] = 'test_op_3';
    $options['multiple'] = TRUE;
    $this->argumentValidator
      ->init($this->executable, $this->display, $options);
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument('1,2'));
    $this
      ->assertTrue($this->argumentValidator
      ->validateArgument('1+2'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTest::$argumentValidator protected property The tested argument validator.
EntityTest::$display protected property The view display.
EntityTest::$entityManager protected property The entity manager.
EntityTest::$executable protected property The view executable.
EntityTest::setUp protected function Overrides UnitTestCase::setUp
EntityTest::testCalculateDependencies public function @covers ::calculateDependencies
EntityTest::testValidateArgumentAccess public function Tests the validate argument method with access and no bundles.
EntityTest::testValidateArgumentBundle public function Tests the validate argument method with bundle checking.
EntityTest::testValidateArgumentMultiple public function Tests the validate argument method with multiple argument splitting.
EntityTest::testValidateArgumentNoAccess public function Tests the validate argument method with no access and bundles.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.