EntityUrlTest.php 9.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\Entity\EntityUrlTest.
 */

namespace Drupal\Tests\Core\Entity;

use Drupal\Core\DependencyInjection\ContainerBuilder;
11
use Drupal\Core\Entity\EntityInterface;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\Core\Entity\Entity
 * @group Entity
 */
class EntityUrlTest extends UnitTestCase {

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

27 28 29 30 31
  /**
   * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $urlGenerator;

32 33 34 35 36 37 38
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
39
    $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
40 41 42

    $container = new ContainerBuilder();
    $container->set('entity.manager', $this->entityManager);
43
    $container->set('url_generator', $this->urlGenerator);
44 45 46 47 48 49 50 51 52 53 54 55
    \Drupal::setContainer($container);
  }

  /**
   * Tests the urlInfo() method.
   *
   * @covers ::urlInfo()
   *
   * @dataProvider providerTestUrlInfo
   */
  public function testUrlInfo($entity_class, $link_template, $expected) {
    /** @var $entity \Drupal\Core\Entity\EntityInterface */
56
    $entity = $this->getMockForAbstractClass($entity_class, array(array('id' => 'test_entity_id'), 'test_entity_type'));
57 58 59 60 61 62 63 64 65 66 67
    $uri = $this->getTestUrlInfo($entity, $link_template);

    $this->assertSame($expected, $uri->getRouteName());
    $this->assertSame($entity, $uri->getOption('entity'));
  }

  /**
   * Provides test data for testUrlInfo().
   */
  public function providerTestUrlInfo() {
    return array(
68 69
      array('Drupal\Core\Entity\Entity', 'edit-form', 'test_entity_type.edit'),
      array('Drupal\Core\Config\Entity\ConfigEntityBase', 'edit-form', 'test_entity_type.edit'),
70
      // Test that overriding the default $rel parameter works.
71
      array('Drupal\Core\Config\Entity\ConfigEntityBase', FALSE, 'test_entity_type.edit'),
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    );
  }

  /**
   * Tests the urlInfo() method with an invalid link template.
   *
   * @covers ::urlInfo()
   *
   * @expectedException \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
   * @expectedExceptionMessage No link template "canonical" found for the "test_entity_type" entity type
   *
   * @dataProvider providerTestUrlInfoForInvalidLinkTemplate
   */
  public function testUrlInfoForInvalidLinkTemplate($entity_class, $link_template) {
    /** @var $entity \Drupal\Core\Entity\EntityInterface */
87
    $entity = $this->getMockForAbstractClass($entity_class, array(array('id' => 'test_entity_id'), 'test_entity_type'));
88 89 90 91 92 93 94 95 96 97
    $uri = $this->getTestUrlInfo($entity, $link_template);

    $this->assertEmpty($uri);
  }

  /**
   * Provides test data for testUrlInfoForInvalidLinkTemplate().
   */
  public function providerTestUrlInfoForInvalidLinkTemplate() {
    return array(
98 99 100
      array('Drupal\Core\Entity\Entity', 'canonical'),
      array('Drupal\Core\Entity\Entity', FALSE),
      array('Drupal\Core\Config\Entity\ConfigEntityBase', 'canonical'),
101 102
    );
  }
103

104 105 106 107 108 109 110 111 112 113 114 115
  /**
   * Creates a \Drupal\Core\Url object based on the entity and link template.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The test entity.
   * @param string $link_template
   *   The link template.
   *
   * @return \Drupal\Core\Url
   *   The URL for this entity's link template.
   */
  protected function getTestUrlInfo(EntityInterface $entity, $link_template) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity_type->expects($this->once())
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'edit-form' => 'test_entity_type.edit',
      )));

    $this->entityManager
      ->expects($this->any())
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

    // If no link template is given, call without a value to test the default.
    if ($link_template) {
      $uri = $entity->urlInfo($link_template);
    }
    else {
      $uri = $entity->urlInfo();
    }

137
    return $uri;
138 139 140 141 142 143 144 145 146 147 148 149
  }

  /**
   * Tests the urlInfo() method when an entity is still "new".
   *
   * @see \Drupal\Core\Entity\EntityInterface::isNew()
   *
   * @covers ::urlInfo()
   *
   * @expectedException \Drupal\Core\Entity\EntityMalformedException
   */
  public function testUrlInfoForNewEntity() {
150
    $entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
151 152 153 154 155 156 157 158 159 160
    $entity->urlInfo();
  }

  /**
   * Tests the url() method.
   *
   * @covers ::url()
   */
  public function testUrl() {
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
161
    $entity_type->expects($this->exactly(5))
162 163 164 165 166 167
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'canonical' => 'test_entity_type.view',
      )));

    $this->entityManager
168
      ->expects($this->exactly(5))
169 170 171 172
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

173
    $invalid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
174 175
    $this->assertSame('', $invalid_entity->url());

176
    $no_link_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
177 178
    $this->assertSame('', $no_link_entity->url('banana'));

179
    $valid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
180
    $this->urlGenerator->expects($this->exactly(2))
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      ->method('generateFromRoute')
      ->will($this->returnValueMap(array(
        array(
          'test_entity_type.view',
          array('test_entity_type' => 'test_entity_id'),
          array('entity_type' => 'test_entity_type', 'entity' => $valid_entity),
          '/entity/test_entity_type/test_entity_id',
        ),
        array(
          'test_entity_type.view',
          array('test_entity_type' => 'test_entity_id'),
          array('absolute' => TRUE, 'entity_type' => 'test_entity_type', 'entity' => $valid_entity),
          'http://drupal/entity/test_entity_type/test_entity_id',
        ),
      )));

    $this->assertSame('/entity/test_entity_type/test_entity_id', $valid_entity->url());
    $this->assertSame('http://drupal/entity/test_entity_type/test_entity_id', $valid_entity->url('canonical', array('absolute' => TRUE)));
  }

  /**
202
   * Tests the getPathByAlias() method.
203
   *
204
   * @covers ::getSystemPath()
205 206 207
   */
  public function testGetSystemPath() {
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
208
    $entity_type->expects($this->exactly(3))
209 210 211 212 213 214 215 216 217 218 219
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'canonical' => 'test_entity_type.view',
      )));

    $this->entityManager
      ->expects($this->exactly(3))
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

220
    $no_link_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
221 222
    $this->assertSame('', $no_link_entity->getSystemPath('banana'));

223
    $this->urlGenerator->expects($this->once())
224 225 226 227
      ->method('getPathFromRoute')
      ->with('test_entity_type.view', array('test_entity_type' => 'test_entity_id'))
      ->will($this->returnValue('entity/test_entity_type/test_entity_id'));

228
    $valid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
229 230 231 232 233 234 235 236 237 238 239 240

    $this->assertSame('entity/test_entity_type/test_entity_id', $valid_entity->getSystemPath());
  }

  /**
   * Tests the retrieval of link templates.
   *
   * @covers ::hasLinkTemplate()
   * @covers ::linkTemplates()
   *
   * @dataProvider providerTestLinkTemplates
   */
241
  public function testLinkTemplates($override_templates, $expected) {
242
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
243
    $entity_type->expects($this->any())
244 245 246 247 248 249
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'canonical' => 'test_entity_type.view',
      )));

    $this->entityManager
250
      ->expects($this->any())
251 252 253 254
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

255 256 257 258 259 260 261 262 263 264
    $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;
      }));
265 266 267 268 269 270 271 272 273
    $this->assertSame($expected['canonical'], $entity->hasLinkTemplate('canonical'));
    $this->assertSame($expected['bananas'], $entity->hasLinkTemplate('bananas'));
  }

  /**
   * Provides test data for testLinkTemplates().
   */
  public function providerTestLinkTemplates() {
    return array(
274
      array(FALSE, array(
275 276 277
        'canonical' => TRUE,
        'bananas' => FALSE,
      )),
278
      array(TRUE, array(
279 280 281 282 283 284 285
        'canonical' => TRUE,
        'bananas' => TRUE,
      )),
    );
  }

}