class EntityTest

Same name in this branch
  1. 10 core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  2. 10 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest
Same name in other branches
  1. 9 core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest
  2. 9 core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  3. 9 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest
  4. 8.9.x core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest
  5. 8.9.x core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  6. 8.9.x core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest
  7. 11.x core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest
  8. 11.x core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  9. 11.x core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest

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

Hierarchy

Expanded class hierarchy of EntityTest

File

core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php, line 16

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 type manager.
     *
     * @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The mocked entity type bundle info used in this test.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeBundleInfo;
    
    /**
     * The tested argument validator.
     *
     * @var \Drupal\views\Plugin\views\argument_validator\Entity
     */
    protected $argumentValidator;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
        $this->entityTypeBundleInfo = $this->createMock(EntityTypeBundleInfoInterface::class);
        $mock_entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityBase', [], '', FALSE, TRUE, TRUE, [
            'bundle',
            'access',
        ]);
        $mock_entity->expects($this->any())
            ->method('bundle')
            ->willReturn('test_bundle');
        $mock_entity->expects($this->any())
            ->method('access')
            ->willReturnMap([
            [
                'test_op',
                NULL,
                FALSE,
                TRUE,
            ],
            [
                'test_op_2',
                NULL,
                FALSE,
                FALSE,
            ],
            [
                'test_op_3',
                NULL,
                FALSE,
                TRUE,
            ],
        ]);
        $mock_entity_bundle_2 = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityBase', [], '', FALSE, TRUE, TRUE, [
            'bundle',
            'access',
        ]);
        $mock_entity_bundle_2->expects($this->any())
            ->method('bundle')
            ->willReturn('test_bundle_2');
        $mock_entity_bundle_2->expects($this->any())
            ->method('access')
            ->willReturnMap([
            [
                'test_op',
                NULL,
                FALSE,
                FALSE,
            ],
            [
                'test_op_2',
                NULL,
                FALSE,
                FALSE,
            ],
            [
                'test_op_3',
                NULL,
                FALSE,
                TRUE,
            ],
        ]);
        $storage = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
        // Setup values for IDs passed as strings or numbers.
        $value_map = [
            [
                [],
                [],
            ],
            [
                [
                    1,
                ],
                [
                    1 => $mock_entity,
                ],
            ],
            [
                [
                    '1',
                ],
                [
                    1 => $mock_entity,
                ],
            ],
            [
                [
                    1,
                    2,
                ],
                [
                    1 => $mock_entity,
                    2 => $mock_entity_bundle_2,
                ],
            ],
            [
                [
                    '1',
                    '2',
                ],
                [
                    1 => $mock_entity,
                    2 => $mock_entity_bundle_2,
                ],
            ],
            [
                [
                    2,
                ],
                [
                    2 => $mock_entity_bundle_2,
                ],
            ],
            [
                [
                    '2',
                ],
                [
                    2 => $mock_entity_bundle_2,
                ],
            ],
        ];
        $storage->expects($this->any())
            ->method('loadMultiple')
            ->willReturnMap($value_map);
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getStorage')
            ->with('entity_test')
            ->willReturn($storage);
        $this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')
            ->disableOriginalConstructor()
            ->getMock();
        $this->display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
            ->disableOriginalConstructor()
            ->getMock();
        $definition = [
            'entity_type' => 'entity_test',
        ];
        $this->argumentValidator = new Entity([], 'entity_test', $definition, $this->entityTypeManager, $this->entityTypeBundleInfo);
    }
    
    /**
     * Tests the validate argument method with no access and bundles.
     *
     * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
     */
    public function testValidateArgumentNoAccess() : void {
        $options = [];
        $options['access'] = FALSE;
        $options['bundles'] = [];
        $this->argumentValidator
            ->init($this->executable, $this->display, $options);
        $this->assertFalse($this->argumentValidator
            ->validateArgument(3));
        $this->assertFalse($this->argumentValidator
            ->validateArgument(''));
        $this->assertFalse($this->argumentValidator
            ->validateArgument(NULL));
        $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() : void {
        $options = [];
        $options['access'] = TRUE;
        $options['bundles'] = [];
        $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 = [];
        $options['access'] = TRUE;
        $options['bundles'] = [];
        $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() : void {
        $options = [];
        $options['access'] = FALSE;
        $options['bundles'] = [
            'test_bundle' => 1,
        ];
        $this->argumentValidator
            ->init($this->executable, $this->display, $options);
        $this->assertTrue($this->argumentValidator
            ->validateArgument(1));
        $this->assertFalse($this->argumentValidator
            ->validateArgument(2));
        $options['bundles'] = NULL;
        $this->argumentValidator
            ->init($this->executable, $this->display, $options);
        $this->assertTrue($this->argumentValidator
            ->validateArgument(1));
        $this->assertTrue($this->argumentValidator
            ->validateArgument(2));
    }
    
    /**
     * @covers ::calculateDependencies
     */
    public function testCalculateDependencies() : void {
        // Create an entity type manager, storage, entity type, and entity to mock the
        // loading of entities providing bundles.
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $storage = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
        $entity_type = $this->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
        $mock_entity = $this->createMock('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');
        $entity_type_manager->expects($this->any())
            ->method('getDefinition')
            ->with('entity_test')
            ->willReturn($entity_type);
        $entity_type_manager->expects($this->any())
            ->method('hasHandler')
            ->with('entity_test_bundle', 'storage')
            ->willReturn(TRUE);
        $entity_type_manager->expects($this->any())
            ->method('getStorage')
            ->with('entity_test_bundle')
            ->willReturn($storage);
        // Set up the argument validator.
        $argumentValidator = new Entity([], 'entity_test', [
            'entity_type' => 'entity_test',
        ], $entity_type_manager, $this->entityTypeBundleInfo);
        $options = [];
        $options['access'] = FALSE;
        $options['bundles'] = [
            '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() : void {
        $options = [];
        $options['access'] = TRUE;
        $options['bundles'] = [];
        $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'));
        $this->assertFalse($this->argumentValidator
            ->validateArgument(NULL));
        $options = [];
        $options['access'] = TRUE;
        $options['bundles'] = [];
        $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

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityTest::$argumentValidator protected property The tested argument validator.
EntityTest::$display protected property The view display.
EntityTest::$entityTypeBundleInfo protected property The mocked entity type bundle info used in this test.
EntityTest::$entityTypeManager protected property The entity type 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.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.