EntityAdapterUnitTest.php 12.6 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\Core\Entity\TypedData;

use Drupal\Core\DependencyInjection\ContainerBuilder;
6 7
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
8 9 10
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
11
use Drupal\Core\TypedData\Exception\MissingDataException;
12
use Drupal\Core\TypedData\TypedDataManagerInterface;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Language\Language;

/**
 * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
 * @group Entity
 * @group TypedData
 */
class EntityAdapterUnitTest extends UnitTestCase {

  /**
   * The bundle used for testing.
   *
   * @var string
   */
  protected $bundle;

  /**
31
   * The content entity used for testing.
32
   *
33
   * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit\Framework\MockObject\MockObject
34 35 36
   */
  protected $entity;

37 38
  /**
   * The content entity adapter under test.
39 40 41 42 43 44 45 46
   *
   * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
   */
  protected $entityAdapter;

  /**
   * The entity type used for testing.
   *
47
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
48 49 50
   */
  protected $entityType;

51 52 53
  /**
   * The entity type manager used for testing.
   *
54
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
55 56 57 58 59
   */
  protected $entityTypeManager;

  /**
   *
60
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit\Framework\MockObject\MockObject
61 62 63
   */
  protected $entityFieldManager;

64 65 66 67 68 69 70 71 72 73
  /**
   * The type ID of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The typed data manager used for testing.
   *
74
   * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit\Framework\MockObject\MockObject
75 76 77 78 79 80
   */
  protected $typedDataManager;

  /**
   * The field item list returned by the typed data manager.
   *
81
   * @var \Drupal\Core\Field\FieldItemListInterface|\PHPUnit\Framework\MockObject\MockObject
82 83 84 85 86 87
   */
  protected $fieldItemList;

  /**
   * The field type manager used for testing.
   *
88
   * @var \Drupal\Core\Field\FieldTypePluginManager|\PHPUnit\Framework\MockObject\MockObject
89 90 91 92 93 94
   */
  protected $fieldTypePluginManager;

  /**
   * The language manager.
   *
95
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
96 97 98 99 100 101
   */
  protected $languageManager;

  /**
   * The UUID generator used for testing.
   *
102
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\MockObject
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
   */
  protected $uuid;

  /**
   * The entity ID.
   *
   * @var int
   */
  protected $id;

  /**
   * Field definitions.
   *
   * @var \Drupal\Core\Field\BaseFieldDefinition[]
   */
  protected $fieldDefinitions;

  /**
   * {@inheritdoc}
   */
123
  protected function setUp(): void {
124
    $this->id = 1;
125
    $values = [
126 127
      'id' => $this->id,
      'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
128 129
      'defaultLangcode' => [LanguageInterface::LANGCODE_DEFAULT => 'en'],
    ];
130 131 132
    $this->entityTypeId = $this->randomMachineName();
    $this->bundle = $this->randomMachineName();

133
    $this->entityType = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
134 135
    $this->entityType->expects($this->any())
      ->method('getKeys')
136
      ->will($this->returnValue([
137 138
        'id' => 'id',
        'uuid' => 'uuid',
139
    ]));
140

141
    $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
142
    $this->entityTypeManager->expects($this->any())
143 144 145 146
      ->method('getDefinition')
      ->with($this->entityTypeId)
      ->will($this->returnValue($this->entityType));

147
    $this->uuid = $this->createMock('\Drupal\Component\Uuid\UuidInterface');
148

149
    $this->typedDataManager = $this->createMock(TypedDataManagerInterface::class);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    $this->typedDataManager->expects($this->any())
      ->method('getDefinition')
      ->with('entity')
      ->will($this->returnValue(['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter']));
    $this->typedDataManager->expects($this->any())
      ->method('getDefaultConstraints')
      ->willReturn([]);

    $validation_constraint_manager = $this->getMockBuilder('\Drupal\Core\Validation\ConstraintManager')
      ->disableOriginalConstructor()
      ->getMock();
    $validation_constraint_manager->expects($this->any())
      ->method('create')
      ->willReturn([]);
    $this->typedDataManager->expects($this->any())
      ->method('getValidationConstraintManager')
      ->willReturn($validation_constraint_manager);

168
    $not_specified = new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'locked' => TRUE]);
169
    $this->languageManager = $this->createMock('\Drupal\Core\Language\LanguageManagerInterface');
170 171
    $this->languageManager->expects($this->any())
      ->method('getLanguages')
172
      ->will($this->returnValue([LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified]));
173 174 175 176 177 178 179 180 181 182 183

    $this->languageManager->expects($this->any())
      ->method('getLanguage')
      ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
      ->will($this->returnValue($not_specified));

    $this->fieldTypePluginManager = $this->getMockBuilder('\Drupal\Core\Field\FieldTypePluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->fieldTypePluginManager->expects($this->any())
      ->method('getDefaultStorageSettings')
184
      ->will($this->returnValue([]));
185 186
    $this->fieldTypePluginManager->expects($this->any())
      ->method('getDefaultFieldSettings')
187
      ->will($this->returnValue([]));
188

189
    $this->fieldItemList = $this->createMock('\Drupal\Core\Field\FieldItemListInterface');
190 191 192 193
    $this->fieldTypePluginManager->expects($this->any())
      ->method('createFieldItemList')
      ->willReturn($this->fieldItemList);

194 195
    $this->entityFieldManager = $this->getMockForAbstractClass(EntityFieldManagerInterface::class);

196
    $container = new ContainerBuilder();
197 198
    $container->set('entity_type.manager', $this->entityTypeManager);
    $container->set('entity_field.manager', $this->entityFieldManager);
199 200 201 202 203 204
    $container->set('uuid', $this->uuid);
    $container->set('typed_data_manager', $this->typedDataManager);
    $container->set('language_manager', $this->languageManager);
    $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
    \Drupal::setContainer($container);

205
    $this->fieldDefinitions = [
206 207
      'id' => BaseFieldDefinition::create('integer'),
      'revision_id' => BaseFieldDefinition::create('integer'),
208
    ];
209
    $this->entityFieldManager->expects($this->any())
210 211 212
      ->method('getFieldDefinitions')
      ->with($this->entityTypeId, $this->bundle)
      ->will($this->returnValue($this->fieldDefinitions));
213

214
    $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]);
215 216 217 218 219 220 221 222

    $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
  }

  /**
   * @covers ::getConstraints
   */
  public function testGetConstraints() {
223
    $this->assertIsArray($this->entityAdapter->getConstraints());
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  }

  /**
   * @covers ::getName
   */
  public function testGetName() {
    $this->assertNull($this->entityAdapter->getName());
  }

  /**
   * @covers ::getRoot
   */
  public function testGetRoot() {
    $this->assertSame(spl_object_hash($this->entityAdapter), spl_object_hash($this->entityAdapter->getRoot()));
  }

  /**
   * @covers ::getPropertyPath
   */
  public function testGetPropertyPath() {
    $this->assertSame('', $this->entityAdapter->getPropertyPath());
  }

  /**
   * @covers ::getParent
   */
  public function testGetParent() {
    $this->assertNull($this->entityAdapter->getParent());
  }

  /**
   * @covers ::setContext
   */
  public function testSetContext() {
    $name = $this->randomMachineName();
259
    $parent = $this->createMock('\Drupal\Core\TypedData\TraversableTypedDataInterface');
260 261 262 263 264 265 266 267 268 269 270 271 272
    // Our mocked entity->setContext() returns NULL, so assert that.
    $this->assertNull($this->entityAdapter->setContext($name, $parent));
    $this->assertEquals($name, $this->entityAdapter->getName());
    $this->assertEquals($parent, $this->entityAdapter->getParent());
  }

  /**
   * @covers ::getValue
   */
  public function testGetValue() {
    $this->assertEquals($this->entity, $this->entityAdapter->getValue());
  }

273 274 275 276 277 278 279
  /**
   * @covers ::getEntity
   */
  public function testGetEntity() {
    $this->assertSame($this->entity, $this->entityAdapter->getEntity());
  }

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  /**
   * @covers ::setValue
   */
  public function testSetValue() {
    $this->entityAdapter->setValue(NULL);
    $this->assertNull($this->entityAdapter->getValue());
  }

  /**
   * @covers ::get
   */
  public function testGet() {
    $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->entityAdapter->get('id'));
  }

  /**
   * @covers ::get
   */
  public function testGetInvalidField() {
299
    $this->expectException(\InvalidArgumentException::class);
300 301 302 303 304 305 306 307
    $this->entityAdapter->get('invalid');
  }

  /**
   * @covers ::get
   */
  public function testGetWithoutData() {
    $this->entityAdapter->setValue(NULL);
308
    $this->expectException(MissingDataException::class);
309 310 311 312 313 314 315
    $this->entityAdapter->get('id');
  }

  /**
   * @covers ::set
   */
  public function testSet() {
316
    $id_items = [['value' => $this->id + 1]];
317 318 319 320 321 322 323 324 325 326 327 328 329

    $this->fieldItemList->expects($this->once())
      ->method('setValue')
      ->with($id_items);

    $this->entityAdapter->set('id', $id_items);
  }

  /**
   * @covers ::set
   */
  public function testSetWithoutData() {
    $this->entityAdapter->setValue(NULL);
330
    $id_items = [['value' => $this->id + 1]];
331
    $this->expectException(MissingDataException::class);
332 333 334 335
    $this->entityAdapter->set('id', $id_items);
  }

  /**
336 337
   * @covers ::getProperties
   */
338 339 340 341 342 343 344 345 346 347 348 349 350 351
  public function testGetProperties() {
    $fields = $this->entityAdapter->getProperties();
    $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['id']);
    $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['revision_id']);
  }

  /**
   * @covers ::toArray
   */
  public function testToArray() {
    $array = $this->entityAdapter->toArray();
    // Mock field objects return NULL values, so test keys only.
    $this->assertArrayHasKey('id', $array);
    $this->assertArrayHasKey('revision_id', $array);
352
    $this->assertCount(2, $array);
353 354 355 356 357 358 359
  }

  /**
   * @covers ::toArray
   */
  public function testToArrayWithoutData() {
    $this->entityAdapter->setValue(NULL);
360
    $this->expectException(MissingDataException::class);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    $this->entityAdapter->toArray();
  }

  /**
   * @covers ::isEmpty
   */
  public function testIsEmpty() {
    $this->assertFalse($this->entityAdapter->isEmpty());
    $this->entityAdapter->setValue(NULL);
    $this->assertTrue($this->entityAdapter->isEmpty());
  }

  /**
   * @covers ::onChange
   */
  public function testOnChange() {
377
    $entity = $this->createMock('\Drupal\Core\Entity\ContentEntityInterface');
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    $entity->expects($this->once())
      ->method('onChange')
      ->with('foo')
      ->willReturn(NULL);
    $this->entityAdapter->setValue($entity);
    $this->entityAdapter->onChange('foo');
  }

  /**
   * @covers ::getDataDefinition
   */
  public function testGetDataDefinition() {
    $definition = $this->entityAdapter->getDataDefinition();
    $this->assertInstanceOf('\Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface', $definition);
    $this->assertEquals($definition->getEntityTypeId(), $this->entityTypeId);
393
    $this->assertEquals($definition->getBundles(), [$this->bundle]);
394 395 396 397 398 399
  }

  /**
   * @covers ::getString
   */
  public function testGetString() {
400
    $entity = $this->createMock('\Drupal\Core\Entity\ContentEntityInterface');
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    $entity->expects($this->once())
      ->method('label')
      ->willReturn('foo');
    $this->entityAdapter->setValue($entity);
    $this->assertEquals('foo', $this->entityAdapter->getString());
    $this->entityAdapter->setValue(NULL);
    $this->assertEquals('', $this->entityAdapter->getString());
  }

  /**
   * @covers ::applyDefaultValue
   */
  public function testApplyDefaultValue() {
    // For each field on the entity the mock method has to be invoked once.
    $this->fieldItemList->expects($this->exactly(2))
      ->method('applyDefaultValue');
    $this->entityAdapter->applyDefaultValue();
  }

  /**
   * @covers ::getIterator
   */
  public function testGetIterator() {
424
    // Content entity test.
425 426 427 428
    $iterator = $this->entityAdapter->getIterator();
    $fields = iterator_to_array($iterator);
    $this->assertArrayHasKey('id', $fields);
    $this->assertArrayHasKey('revision_id', $fields);
429
    $this->assertCount(2, $fields);
430 431 432 433

    $this->entityAdapter->setValue(NULL);
    $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator());
  }
434

435
}