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() {
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$mock_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity')
->disableOriginalConstructor()
->setMethods(array('bundle', 'access'))
->getMock();
$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'));
......@@ -79,10 +76,7 @@ protected function setUp() {
array('test_op_3', NULL, TRUE),
)));
$mock_entity_bundle_2 = $this->getMockBuilder('Drupal\Core\Entity\Entity')
->disableOriginalConstructor()
->setMethods(array('bundle', 'access'))
->getMock();
$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'));
......
......@@ -7,9 +7,7 @@
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Tests\UnitTestCase;
......@@ -70,7 +68,7 @@ protected function setUp() {
*/
public function testUrlInfo($entity_class, $link_template, $expected) {
/** @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);
$this->assertSame($expected, $uri->getRouteName());
......@@ -82,10 +80,10 @@ public function testUrlInfo($entity_class, $link_template, $expected) {
*/
public function providerTestUrlInfo() {
return array(
array('Drupal\Tests\Core\Entity\TestEntity', 'edit-form', 'test_entity_type.edit'),
array('Drupal\Tests\Core\Entity\TestConfigEntity', 'edit-form', 'test_entity_type.edit'),
array('Drupal\Core\Entity\Entity', '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.
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() {
*/
public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template) {
/** @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);
$this->assertEmpty($uri);
......@@ -112,9 +110,9 @@ public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template)
*/
public function providerTestUrlInfoForInvalidLinkTemplate() {
return array(
array('Drupal\Tests\Core\Entity\TestEntity', 'canonical'),
array('Drupal\Tests\Core\Entity\TestEntity', FALSE),
array('Drupal\Tests\Core\Entity\TestConfigEntity', 'canonical'),
array('Drupal\Core\Entity\Entity', 'canonical'),
array('Drupal\Core\Entity\Entity', FALSE),
array('Drupal\Core\Config\Entity\ConfigEntityBase', 'canonical'),
);
}
......@@ -164,7 +162,7 @@ protected function getTestUrlInfo(EntityInterface $entity, $link_template) {
* @expectedException \Drupal\Core\Entity\EntityMalformedException
*/
public function testUrlInfoForNewEntity() {
$entity = new TestEntity(array(), 'test_entity_type');
$entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
$entity->urlInfo();
}
......@@ -187,13 +185,13 @@ public function testUrl() {
->with('test_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());
$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'));
$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))
->method('generateFromRoute')
->will($this->returnValueMap(array(
......@@ -245,7 +243,10 @@ public function testUrlForAdminForm() {
))
->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'));
}
......@@ -269,7 +270,7 @@ public function testGetSystemPath() {
->with('test_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->urlGenerator->expects($this->once())
......@@ -277,7 +278,7 @@ public function testGetSystemPath() {
->with('test_entity_type.view', array('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());
}
......@@ -290,21 +291,30 @@ public function testGetSystemPath() {
*
* @dataProvider providerTestLinkTemplates
*/
public function testLinkTemplates($entity_class, $expected) {
public function testLinkTemplates($override_templates, $expected) {
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type->expects($this->exactly(2))
$entity_type->expects($this->any())
->method('getLinkTemplates')
->will($this->returnValue(array(
'canonical' => 'test_entity_type.view',
)));
$this->entityManager
->expects($this->exactly(2))
->expects($this->any())
->method('getDefinition')
->with('test_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['bananas'], $entity->hasLinkTemplate('bananas'));
}
......@@ -314,11 +324,11 @@ public function testLinkTemplates($entity_class, $expected) {
*/
public function providerTestLinkTemplates() {
return array(
array('Drupal\Tests\Core\Entity\TestEntity', array(
array(FALSE, array(
'canonical' => TRUE,
'bananas' => FALSE,
)),
array('Drupal\Tests\Core\Entity\TestEntityWithTemplates', array(
array(TRUE, array(
'canonical' => TRUE,
'bananas' => TRUE,
)),
......@@ -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