ConfigEntityMapperTest.php 5.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

/**
 * @file
 * Contains \Drupal\config_translation\Tests\ConfigEntityMapperTest.
 */

namespace Drupal\config_translation\Tests;

use Drupal\config_translation\ConfigEntityMapper;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;

/**
15
 * Tests the functionality provided by the configuration entity mapper.
16
 *
17
 * @group config_translation
18 19 20 21 22 23 24 25 26 27 28 29 30
 */
class ConfigEntityMapperTest extends UnitTestCase {

  /**
   * The configuration entity mapper to test.
   *
   * @var \Drupal\config_translation\ConfigEntityMapper
   */
  protected $configEntityMapper;

  /**
   * The entity manager used for testing.
   *
31
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32 33 34 35 36 37
   */
  protected $entityManager;

  /**
   * The entity instance used for testing.
   *
38
   * @var \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
39 40 41 42 43 44 45 46 47 48 49
   */
  protected $entity;

  /**
   * The route provider used for testing.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $routeProvider;

  public function setUp() {
50
    $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
51 52 53 54 55 56

    $this->entity = $this->getMock('Drupal\Core\Entity\EntityInterface');

    $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');

    $this->routeProvider
57
      ->expects($this->any())
58
      ->method('getRouteByName')
59
      ->with('entity.language_entity.edit_form')
60 61 62 63
      ->will($this->returnValue(new Route('/admin/config/regional/language/edit/{language_entity}')));

    $definition = array(
      'class' => '\Drupal\config_translation\ConfigEntityMapper',
64
      'base_route_name' => 'entity.language_entity.edit_form',
65 66 67
      'title' => '!label language',
      'names' => array(),
      'entity_type' => 'language_entity',
68
      'route_name' => 'config_translation.item.overview.entity.language_entity.edit_form',
69
    );
70 71 72

    $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');

73 74 75 76 77 78 79 80
    $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
      ->disableOriginalConstructor()
      ->getMock();

    $this->configEntityMapper = new ConfigEntityMapper(
      'language_entity',
      $definition,
      $this->getConfigFactoryStub(),
81
      $typed_config_manager,
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      $locale_config_manager,
      $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
      $this->routeProvider,
      $this->getStringTranslationStub(),
      $this->entityManager
    );
  }

  /**
   * Tests ConfigEntityMapper::setEntity().
   */
  public function testSetEntity() {
    $this->entity
      ->expects($this->once())
      ->method('id')
      ->with()
      ->will($this->returnValue('entity_id'));

100
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
101 102 103 104
    $this->entityManager
      ->expects($this->once())
      ->method('getDefinition')
      ->with('language_entity')
105
      ->will($this->returnValue($entity_type));
106 107 108 109 110 111 112 113 114 115 116 117 118

    $result = $this->configEntityMapper->setEntity($this->entity);
    $this->assertTrue($result);

    // Make sure setEntity() returns FALSE when called a second time.
    $result = $this->configEntityMapper->setEntity($this->entity);
    $this->assertFalse($result);
  }

  /**
   * Tests ConfigEntityMapper::getOverviewRouteParameters().
   */
  public function testGetOverviewRouteParameters() {
119 120 121 122 123 124
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $this->entityManager
      ->expects($this->once())
      ->method('getDefinition')
      ->with('language_entity')
      ->will($this->returnValue($entity_type));
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    $this->configEntityMapper->setEntity($this->entity);

    $this->entity
      ->expects($this->once())
      ->method('id')
      ->with()
      ->will($this->returnValue('entity_id'));

    $result = $this->configEntityMapper->getOverviewRouteParameters();

    $this->assertSame(array('language_entity' => 'entity_id'), $result);
  }

  /**
   * Tests ConfigEntityMapper::getType().
   */
  public function testGetType() {
    $result = $this->configEntityMapper->getType();
    $this->assertSame('language_entity', $result);
  }

  /**
   * Tests ConfigEntityMapper::getTypeName().
   */
  public function testGetTypeName() {
150 151 152 153
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity_type->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('test'));
154 155 156 157
    $this->entityManager
      ->expects($this->once())
      ->method('getDefinition')
      ->with('language_entity')
158
      ->will($this->returnValue($entity_type));
159 160 161 162 163 164 165 166 167

    $result = $this->configEntityMapper->getTypeName();
    $this->assertSame('test', $result);
  }

  /**
   * Tests ConfigEntityMapper::getTypeLabel().
   */
  public function testGetTypeLabel() {
168 169 170 171
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity_type->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('test'));
172 173 174 175
    $this->entityManager
      ->expects($this->once())
      ->method('getDefinition')
      ->with('language_entity')
176
      ->will($this->returnValue($entity_type));
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    $result = $this->configEntityMapper->getTypeLabel();
    $this->assertSame('test', $result);
  }

  /**
   * Tests ConfigEntityMapper::getOperations().
   */
  public function testGetOperations() {
    $result = $this->configEntityMapper->getOperations();

    $expected = array(
      'list' => array(
        'title' => 'List',
        'href' => 'admin/config/regional/config-translation/language_entity',
      )
    );

    $this->assertSame($expected, $result);
  }

}