Commit a167eca3 authored by alexpott's avatar alexpott

Issue #2223423 by Xano, tim.plunkett: Stop extending \Drupal\Core\Entity\Entity in unit tests.

parent ed8dfae0
...@@ -64,10 +64,7 @@ protected function setUp() { ...@@ -64,10 +64,7 @@ protected function setUp() {
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$mock_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity') $mock_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(), '', FALSE, TRUE, TRUE, array('bundle', 'access'));
->disableOriginalConstructor()
->setMethods(array('bundle', 'access'))
->getMock();
$mock_entity->expects($this->any()) $mock_entity->expects($this->any())
->method('bundle') ->method('bundle')
->will($this->returnValue('test_bundle')); ->will($this->returnValue('test_bundle'));
...@@ -79,10 +76,7 @@ protected function setUp() { ...@@ -79,10 +76,7 @@ protected function setUp() {
array('test_op_3', NULL, TRUE), array('test_op_3', NULL, TRUE),
))); )));
$mock_entity_bundle_2 = $this->getMockBuilder('Drupal\Core\Entity\Entity') $mock_entity_bundle_2 = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(), '', FALSE, TRUE, TRUE, array('bundle', 'access'));
->disableOriginalConstructor()
->setMethods(array('bundle', 'access'))
->getMock();
$mock_entity_bundle_2->expects($this->any()) $mock_entity_bundle_2->expects($this->any())
->method('bundle') ->method('bundle')
->will($this->returnValue('test_bundle_2')); ->will($this->returnValue('test_bundle_2'));
......
...@@ -7,9 +7,7 @@ ...@@ -7,9 +7,7 @@
namespace Drupal\Tests\Core\Entity; namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
...@@ -70,7 +68,7 @@ protected function setUp() { ...@@ -70,7 +68,7 @@ protected function setUp() {
*/ */
public function testUrlInfo($entity_class, $link_template, $expected) { public function testUrlInfo($entity_class, $link_template, $expected) {
/** @var $entity \Drupal\Core\Entity\EntityInterface */ /** @var $entity \Drupal\Core\Entity\EntityInterface */
$entity = new $entity_class(array('id' => 'test_entity_id'), 'test_entity_type'); $entity = $this->getMockForAbstractClass($entity_class, array(array('id' => 'test_entity_id'), 'test_entity_type'));
$uri = $this->getTestUrlInfo($entity, $link_template); $uri = $this->getTestUrlInfo($entity, $link_template);
$this->assertSame($expected, $uri->getRouteName()); $this->assertSame($expected, $uri->getRouteName());
...@@ -82,10 +80,10 @@ public function testUrlInfo($entity_class, $link_template, $expected) { ...@@ -82,10 +80,10 @@ public function testUrlInfo($entity_class, $link_template, $expected) {
*/ */
public function providerTestUrlInfo() { public function providerTestUrlInfo() {
return array( return array(
array('Drupal\Tests\Core\Entity\TestEntity', 'edit-form', 'test_entity_type.edit'), array('Drupal\Core\Entity\Entity', 'edit-form', 'test_entity_type.edit'),
array('Drupal\Tests\Core\Entity\TestConfigEntity', 'edit-form', 'test_entity_type.edit'), array('Drupal\Core\Config\Entity\ConfigEntityBase', 'edit-form', 'test_entity_type.edit'),
// Test that overriding the default $rel parameter works. // Test that overriding the default $rel parameter works.
array('Drupal\Tests\Core\Entity\TestConfigEntity', FALSE, 'test_entity_type.edit'), array('Drupal\Core\Config\Entity\ConfigEntityBase', FALSE, 'test_entity_type.edit'),
); );
} }
...@@ -101,7 +99,7 @@ public function providerTestUrlInfo() { ...@@ -101,7 +99,7 @@ public function providerTestUrlInfo() {
*/ */
public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template) { public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template) {
/** @var $entity \Drupal\Core\Entity\EntityInterface */ /** @var $entity \Drupal\Core\Entity\EntityInterface */
$entity = new $entity_class(array('id' => 'test_entity_id'), 'test_entity_type'); $entity = $this->getMockForAbstractClass($entity_class, array(array('id' => 'test_entity_id'), 'test_entity_type'));
$uri = $this->getTestUrlInfo($entity, $link_template); $uri = $this->getTestUrlInfo($entity, $link_template);
$this->assertEmpty($uri); $this->assertEmpty($uri);
...@@ -112,9 +110,9 @@ public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template) ...@@ -112,9 +110,9 @@ public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template)
*/ */
public function providerTestUrlInfoForInvalidLinkTemplate() { public function providerTestUrlInfoForInvalidLinkTemplate() {
return array( return array(
array('Drupal\Tests\Core\Entity\TestEntity', 'canonical'), array('Drupal\Core\Entity\Entity', 'canonical'),
array('Drupal\Tests\Core\Entity\TestEntity', FALSE), array('Drupal\Core\Entity\Entity', FALSE),
array('Drupal\Tests\Core\Entity\TestConfigEntity', 'canonical'), array('Drupal\Core\Config\Entity\ConfigEntityBase', 'canonical'),
); );
} }
...@@ -164,7 +162,7 @@ protected function getTestUrlInfo(EntityInterface $entity, $link_template) { ...@@ -164,7 +162,7 @@ protected function getTestUrlInfo(EntityInterface $entity, $link_template) {
* @expectedException \Drupal\Core\Entity\EntityMalformedException * @expectedException \Drupal\Core\Entity\EntityMalformedException
*/ */
public function testUrlInfoForNewEntity() { public function testUrlInfoForNewEntity() {
$entity = new TestEntity(array(), 'test_entity_type'); $entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
$entity->urlInfo(); $entity->urlInfo();
} }
...@@ -187,13 +185,13 @@ public function testUrl() { ...@@ -187,13 +185,13 @@ public function testUrl() {
->with('test_entity_type') ->with('test_entity_type')
->will($this->returnValue($entity_type)); ->will($this->returnValue($entity_type));
$invalid_entity = new TestEntity(array(), 'test_entity_type'); $invalid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
$this->assertSame('', $invalid_entity->url()); $this->assertSame('', $invalid_entity->url());
$no_link_entity = new TestEntity(array('id' => 'test_entity_id'), 'test_entity_type'); $no_link_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
$this->assertSame('', $no_link_entity->url('banana')); $this->assertSame('', $no_link_entity->url('banana'));
$valid_entity = new TestEntity(array('id' => 'test_entity_id'), 'test_entity_type'); $valid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
$this->urlGenerator->expects($this->exactly(2)) $this->urlGenerator->expects($this->exactly(2))
->method('generateFromRoute') ->method('generateFromRoute')
->will($this->returnValueMap(array( ->will($this->returnValueMap(array(
...@@ -245,7 +243,10 @@ public function testUrlForAdminForm() { ...@@ -245,7 +243,10 @@ public function testUrlForAdminForm() {
)) ))
->will($this->returnValue('entity/test_entity_type/test_entity_bundle/test_entity_id')); ->will($this->returnValue('entity/test_entity_type/test_entity_bundle/test_entity_id'));
$entity = new TestEntityWithBundle(array('id' => 'test_entity_id', 'bundle' => 'test_entity_bundle'), 'test_entity_type'); $entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'), '', TRUE, TRUE, TRUE, array('bundle'));
$entity->expects($this->any())
->method('bundle')
->will($this->returnValue('test_entity_bundle'));
$this->assertSame('entity/test_entity_type/test_entity_bundle/test_entity_id', $entity->url('admin-form')); $this->assertSame('entity/test_entity_type/test_entity_bundle/test_entity_id', $entity->url('admin-form'));
} }
...@@ -269,7 +270,7 @@ public function testGetSystemPath() { ...@@ -269,7 +270,7 @@ public function testGetSystemPath() {
->with('test_entity_type') ->with('test_entity_type')
->will($this->returnValue($entity_type)); ->will($this->returnValue($entity_type));
$no_link_entity = new TestEntity(array('id' => 'test_entity_id'), 'test_entity_type'); $no_link_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
$this->assertSame('', $no_link_entity->getSystemPath('banana')); $this->assertSame('', $no_link_entity->getSystemPath('banana'));
$this->urlGenerator->expects($this->once()) $this->urlGenerator->expects($this->once())
...@@ -277,7 +278,7 @@ public function testGetSystemPath() { ...@@ -277,7 +278,7 @@ public function testGetSystemPath() {
->with('test_entity_type.view', array('test_entity_type' => 'test_entity_id')) ->with('test_entity_type.view', array('test_entity_type' => 'test_entity_id'))
->will($this->returnValue('entity/test_entity_type/test_entity_id')); ->will($this->returnValue('entity/test_entity_type/test_entity_id'));
$valid_entity = new TestEntity(array('id' => 'test_entity_id'), 'test_entity_type'); $valid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
$this->assertSame('entity/test_entity_type/test_entity_id', $valid_entity->getSystemPath()); $this->assertSame('entity/test_entity_type/test_entity_id', $valid_entity->getSystemPath());
} }
...@@ -290,21 +291,30 @@ public function testGetSystemPath() { ...@@ -290,21 +291,30 @@ public function testGetSystemPath() {
* *
* @dataProvider providerTestLinkTemplates * @dataProvider providerTestLinkTemplates
*/ */
public function testLinkTemplates($entity_class, $expected) { public function testLinkTemplates($override_templates, $expected) {
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type->expects($this->exactly(2)) $entity_type->expects($this->any())
->method('getLinkTemplates') ->method('getLinkTemplates')
->will($this->returnValue(array( ->will($this->returnValue(array(
'canonical' => 'test_entity_type.view', 'canonical' => 'test_entity_type.view',
))); )));
$this->entityManager $this->entityManager
->expects($this->exactly(2)) ->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with('test_entity_type') ->with('test_entity_type')
->will($this->returnValue($entity_type)); ->will($this->returnValue($entity_type));
$entity = new $entity_class(array('id' => 'test_entity_id'), 'test_entity_type'); $entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'), '', TRUE, TRUE, TRUE, array('linkTemplates'));
$entity->expects($this->any())
->method('linkTemplates')
->will($this->returnCallback(function () use ($entity_type, $override_templates) {
$templates = $entity_type->getLinkTemplates();
if ($override_templates) {
$templates['bananas'] = 'test_entity_type.bananas';
}
return $templates;
}));
$this->assertSame($expected['canonical'], $entity->hasLinkTemplate('canonical')); $this->assertSame($expected['canonical'], $entity->hasLinkTemplate('canonical'));
$this->assertSame($expected['bananas'], $entity->hasLinkTemplate('bananas')); $this->assertSame($expected['bananas'], $entity->hasLinkTemplate('bananas'));
} }
...@@ -314,11 +324,11 @@ public function testLinkTemplates($entity_class, $expected) { ...@@ -314,11 +324,11 @@ public function testLinkTemplates($entity_class, $expected) {
*/ */
public function providerTestLinkTemplates() { public function providerTestLinkTemplates() {
return array( return array(
array('Drupal\Tests\Core\Entity\TestEntity', array( array(FALSE, array(
'canonical' => TRUE, 'canonical' => TRUE,
'bananas' => FALSE, 'bananas' => FALSE,
)), )),
array('Drupal\Tests\Core\Entity\TestEntityWithTemplates', array( array(TRUE, array(
'canonical' => TRUE, 'canonical' => TRUE,
'bananas' => TRUE, 'bananas' => TRUE,
)), )),
...@@ -326,41 +336,3 @@ public function providerTestLinkTemplates() { ...@@ -326,41 +336,3 @@ public function providerTestLinkTemplates() {
} }
} }
class TestConfigEntity extends ConfigEntityBase {
}
class TestEntity extends Entity {
}
class TestEntityWithTemplates extends TestEntity {
/**
* {@inheritdoc}
*/
protected function linkTemplates() {
$templates = parent::linkTemplates();
$templates['bananas'] = 'test_entity_type.bananas';
return $templates;
}
}
class TestEntityWithBundle extends TestEntity {
/**
* The entity bundle.
*
* @var string
*/
protected $bundle;
/**
* {@inheritdoc}
*/
public function bundle() {
return $this->bundle;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment