EntityUrlTest.php 11.1 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 27 28 29 30
use Drupal\Tests\UnitTestCase;

/**
 * Tests the \Drupal\Core\Entity\EntityInterface URL methods.
 *
 * @coversDefaultClass \Drupal\Core\Entity\Entity
 *
 * @group Drupal
 * @group Entity
 */
class EntityUrlTest extends UnitTestCase {

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

31 32 33 34 35
  /**
   * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $urlGenerator;

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'EntityInterface URL test',
      'description' => 'Unit test the EntityInterface URL methods.',
      'group' => 'Entity',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
54
    $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
55 56 57

    $container = new ContainerBuilder();
    $container->set('entity.manager', $this->entityManager);
58
    $container->set('url_generator', $this->urlGenerator);
59 60 61 62 63 64 65 66 67 68 69 70
    \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 */
71
    $entity = $this->getMockForAbstractClass($entity_class, array(array('id' => 'test_entity_id'), 'test_entity_type'));
72 73 74 75 76 77 78 79 80 81 82
    $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(
83 84
      array('Drupal\Core\Entity\Entity', 'edit-form', 'test_entity_type.edit'),
      array('Drupal\Core\Config\Entity\ConfigEntityBase', 'edit-form', 'test_entity_type.edit'),
85
      // Test that overriding the default $rel parameter works.
86
      array('Drupal\Core\Config\Entity\ConfigEntityBase', FALSE, 'test_entity_type.edit'),
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    );
  }

  /**
   * 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 */
102
    $entity = $this->getMockForAbstractClass($entity_class, array(array('id' => 'test_entity_id'), 'test_entity_type'));
103 104 105 106 107 108 109 110 111 112
    $uri = $this->getTestUrlInfo($entity, $link_template);

    $this->assertEmpty($uri);
  }

  /**
   * Provides test data for testUrlInfoForInvalidLinkTemplate().
   */
  public function providerTestUrlInfoForInvalidLinkTemplate() {
    return array(
113 114 115
      array('Drupal\Core\Entity\Entity', 'canonical'),
      array('Drupal\Core\Entity\Entity', FALSE),
      array('Drupal\Core\Config\Entity\ConfigEntityBase', 'canonical'),
116 117
    );
  }
118

119 120 121 122 123 124 125 126 127 128 129 130
  /**
   * 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) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    $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();
    }

152
    return $uri;
153 154 155 156 157 158 159 160 161 162 163 164
  }

  /**
   * 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() {
165
    $entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
166 167 168 169 170 171 172 173 174 175
    $entity->urlInfo();
  }

  /**
   * Tests the url() method.
   *
   * @covers ::url()
   */
  public function testUrl() {
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
176
    $entity_type->expects($this->exactly(5))
177 178 179 180 181 182
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'canonical' => 'test_entity_type.view',
      )));

    $this->entityManager
183
      ->expects($this->exactly(5))
184 185 186 187
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

188
    $invalid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array(), 'test_entity_type'));
189 190
    $this->assertSame('', $invalid_entity->url());

191
    $no_link_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
192 193
    $this->assertSame('', $no_link_entity->url('banana'));

194
    $valid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
195
    $this->urlGenerator->expects($this->exactly(2))
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      ->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)));
  }

  /**
   * Tests the url() method for "admin-form".
   *
   * @covers ::urlRouteParameters()
   */
  public function testUrlForAdminForm() {
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
223
    $entity_type->expects($this->exactly(2))
224 225 226 227 228 229 230 231 232
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'admin-form' => 'test_entity_type.admin_form',
      )));
    $entity_type->expects($this->exactly(2))
      ->method('getBundleEntityType')
      ->will($this->returnValue('test_entity_type_bundle'));

    $this->entityManager
233
      ->expects($this->exactly(4))
234 235 236 237
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

238
    $this->urlGenerator->expects($this->once())
239 240 241 242 243 244 245
      ->method('generateFromRoute')
      ->with('test_entity_type.admin_form', array(
        'test_entity_type_bundle' => 'test_entity_bundle',
        'test_entity_type' => 'test_entity_id',
      ))
      ->will($this->returnValue('entity/test_entity_type/test_entity_bundle/test_entity_id'));

246 247 248 249
    $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'));
250 251 252 253 254

    $this->assertSame('entity/test_entity_type/test_entity_bundle/test_entity_id', $entity->url('admin-form'));
  }

  /**
255
   * Tests the getPathByAlias() method.
256
   *
257
   * @covers ::getPathByAlias()
258 259 260
   */
  public function testGetSystemPath() {
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
261
    $entity_type->expects($this->exactly(3))
262 263 264 265 266 267 268 269 270 271 272
      ->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));

273
    $no_link_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
274 275
    $this->assertSame('', $no_link_entity->getSystemPath('banana'));

276
    $this->urlGenerator->expects($this->once())
277 278 279 280
      ->method('getPathFromRoute')
      ->with('test_entity_type.view', array('test_entity_type' => 'test_entity_id'))
      ->will($this->returnValue('entity/test_entity_type/test_entity_id'));

281
    $valid_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type'));
282 283 284 285 286 287 288 289 290 291 292 293

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

  /**
   * Tests the retrieval of link templates.
   *
   * @covers ::hasLinkTemplate()
   * @covers ::linkTemplates()
   *
   * @dataProvider providerTestLinkTemplates
   */
294
  public function testLinkTemplates($override_templates, $expected) {
295
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
296
    $entity_type->expects($this->any())
297 298 299 300 301 302
      ->method('getLinkTemplates')
      ->will($this->returnValue(array(
        'canonical' => 'test_entity_type.view',
      )));

    $this->entityManager
303
      ->expects($this->any())
304 305 306 307
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($entity_type));

308 309 310 311 312 313 314 315 316 317
    $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;
      }));
318 319 320 321 322 323 324 325 326
    $this->assertSame($expected['canonical'], $entity->hasLinkTemplate('canonical'));
    $this->assertSame($expected['bananas'], $entity->hasLinkTemplate('bananas'));
  }

  /**
   * Provides test data for testLinkTemplates().
   */
  public function providerTestLinkTemplates() {
    return array(
327
      array(FALSE, array(
328 329 330
        'canonical' => TRUE,
        'bananas' => FALSE,
      )),
331
      array(TRUE, array(
332 333 334 335 336 337 338
        'canonical' => TRUE,
        'bananas' => TRUE,
      )),
    );
  }

}