MenuLinkTest.php 2.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php

/**
 * @file
 * Contains \Drupal\menu_link\Tests\Plugin\Core\Entity\MenuLinkTest.
 */

namespace Drupal\menu_link\Tests\Plugin\Core\Entity;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\menu_link\Entity\MenuLink;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

/**
 * Tests the menu link entity class.
 *
 * @group Drupal
 * @group Drupal_menu
 *
 * @see \Drupal\menu_link\Plugin\Core\Entity\MenuLink
 */
class MenuLinkTest extends UnitTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Menu link entity',
      'description' => 'Tests the menu link entity class.',
      'group' => 'Menu',
    );
  }

  /**
   * Tests the findRouteNameParameters method.
   *
   * @see \Drupal\menu_link\Plugin\Core\Entity\MenuLink::findRouteNameParameters()
   */
  public function testFindRouteNameParameters() {
    $router = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
    $container = new ContainerBuilder();
43
    $container->set('router', $router);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    \Drupal::setContainer($container);

    $router->expects($this->at(0))
      ->method('matchRequest')
      ->will($this->returnValue(array(
        RouteObjectInterface::ROUTE_NAME => 'view.frontpage.page_1',
        '_raw_variables' => new ParameterBag(),
      )));
    $router->expects($this->at(1))
      ->method('matchRequest')
      ->will($this->returnValue(array(
        RouteObjectInterface::ROUTE_NAME => 'node_view',
        '_raw_variables' => new ParameterBag(array('node' => '1')),
      )));
    $router->expects($this->at(2))
      ->method('matchRequest')
      ->will($this->returnValue(array(
        RouteObjectInterface::ROUTE_NAME => 'node_edit',
        '_raw_variables' => new ParameterBag(array('node' => '2')),
      )));
    $router->expects($this->at(3))
      ->method('matchRequest')
      ->will($this->throwException(new ResourceNotFoundException()));

    $this->assertEquals(array('view.frontpage.page_1', array()), MenuLink::findRouteNameParameters('node'));
    $this->assertEquals(array('node_view', array('node' => '1')), MenuLink::findRouteNameParameters('node/1'));
    $this->assertEquals(array('node_edit', array('node' => '2')), MenuLink::findRouteNameParameters('node/2/edit'));

    $this->assertEquals(array(), MenuLink::findRouteNameParameters('non-existing'));
  }

}