ConfigEntityStorageTest.php 30 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\Core\Config\Entity;
4

5
use Drupal\Core\Cache\Cache;
6 7 8 9
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11 12 13

/**
 * @coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityStorage
14
 * @group Config
15 16 17 18 19 20
 */
class ConfigEntityStorageTest extends UnitTestCase {

  /**
   * The entity type.
   *
21
   * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
22 23 24
   */
  protected $entityType;

25 26 27 28 29 30 31
  /**
   * The type ID of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The UUID service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $uuidService;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $languageManager;

  /**
54
   * The config storage.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
   */
  protected $entityStorage;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * The entity query.
   *
   * @var \Drupal\Core\Entity\Query\QueryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityQuery;

74 75 76 77 78 79 80
  /**
   * The entity manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

81 82 83
  /**
   * The mocked cache backend.
   *
84
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
85
   */
86
  protected $cacheTagsInvalidator;
87

88 89 90 91 92 93 94
  /**
   * The mocked typed config manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $typedConfigManager;

95 96 97 98 99 100 101
  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configManager;

102 103 104 105 106 107 108
  /**
   * The cache contexts manager.
   *
   * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheContextsManager;

109 110
  /**
   * {@inheritdoc}
111
   *
112
   * @covers ::__construct
113 114 115 116
   */
  protected function setUp() {
    parent::setUp();

117
    $this->entityType = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
118
    $this->entityTypeId = 'test_entity_type';
119 120 121 122 123
    $this->entityType->expects($this->any())
      ->method('getKey')
      ->will($this->returnValueMap(array(
        array('id', 'id'),
        array('uuid', 'uuid'),
124
        array('langcode', 'langcode'),
125 126 127
      )));
    $this->entityType->expects($this->any())
      ->method('id')
128
      ->will($this->returnValue($this->entityTypeId));
129 130 131
    $this->entityType->expects($this->any())
      ->method('getConfigPrefix')
      ->will($this->returnValue('the_config_prefix'));
132 133 134
    $this->entityType->expects($this->any())
      ->method('getClass')
      ->will($this->returnValue(get_class($this->getMockEntity())));
135 136 137
    $this->entityType->expects($this->any())
      ->method('getListCacheTags')
      ->willReturn(array('test_entity_type_list'));
138 139 140 141 142 143 144

    $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');

    $this->uuidService = $this->getMock('Drupal\Component\Uuid\UuidInterface');

    $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
    $this->languageManager->expects($this->any())
145 146
      ->method('getCurrentLanguage')
      ->willReturn(new Language(array('id' => 'hu')));
147 148 149 150 151 152

    $this->configFactory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');

    $this->entityQuery = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');

    $this->entityStorage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
153
      ->setConstructorArgs(array($this->entityType, $this->configFactory, $this->uuidService, $this->languageManager))
154 155 156 157 158 159
      ->setMethods(array('getQuery'))
      ->getMock();
    $this->entityStorage->expects($this->any())
      ->method('getQuery')
      ->will($this->returnValue($this->entityQuery));
    $this->entityStorage->setModuleHandler($this->moduleHandler);
160 161 162 163 164 165 166

    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
    $this->entityManager->expects($this->any())
      ->method('getDefinition')
      ->with('test_entity_type')
      ->will($this->returnValue($this->entityType));

167
    $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
168

169 170 171 172
    $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
    $this->typedConfigManager->expects($this->any())
      ->method('getDefinition')
      ->will($this->returnValue(array('mapping' => array('id' => '', 'uuid' => '', 'dependencies' => ''))));
173 174 175

    $this->configManager = $this->getMock('Drupal\Core\Config\ConfigManagerInterface');

176 177 178 179
    $this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
      ->disableOriginalConstructor()
      ->getMock();

180 181
    $container = new ContainerBuilder();
    $container->set('entity.manager', $this->entityManager);
182
    $container->set('config.typed', $this->typedConfigManager);
183
    $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
184
    $container->set('config.manager', $this->configManager);
185
    $container->set('language_manager', $this->languageManager);
186
    $container->set('cache_contexts_manager', $this->cacheContextsManager);
187 188
    \Drupal::setContainer($container);

189 190 191
  }

  /**
192 193
   * @covers ::create
   * @covers ::doCreate
194 195
   */
  public function testCreateWithPredefinedUuid() {
196
    $this->cacheTagsInvalidator->expects($this->never())
197 198
      ->method('invalidateTags');

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    $this->moduleHandler->expects($this->at(0))
      ->method('invokeAll')
      ->with('test_entity_type_create');
    $this->moduleHandler->expects($this->at(1))
      ->method('invokeAll')
      ->with('entity_create');
    $this->uuidService->expects($this->never())
      ->method('generate');

    $entity = $this->entityStorage->create(array('id' => 'foo', 'uuid' => 'baz'));
    $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity);
    $this->assertSame('foo', $entity->id());
    $this->assertSame('baz', $entity->uuid());
  }

  /**
215 216
   * @covers ::create
   * @covers ::doCreate
217 218 219 220
   *
   * @return \Drupal\Core\Entity\EntityInterface
   */
  public function testCreate() {
221
    $this->cacheTagsInvalidator->expects($this->never())
222 223
      ->method('invalidateTags');

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    $this->moduleHandler->expects($this->at(0))
      ->method('invokeAll')
      ->with('test_entity_type_create');
    $this->moduleHandler->expects($this->at(1))
      ->method('invokeAll')
      ->with('entity_create');
    $this->uuidService->expects($this->once())
      ->method('generate')
      ->will($this->returnValue('bar'));

    $entity = $this->entityStorage->create(array('id' => 'foo'));
    $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity);
    $this->assertSame('foo', $entity->id());
    $this->assertSame('bar', $entity->uuid());
    return $entity;
  }

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  /**
   * @covers ::create
   * @covers ::doCreate
   */
  public function testCreateWithCurrentLanguage() {
    $this->languageManager->expects($this->any())
      ->method('getLanguage')
      ->with('hu')
      ->willReturn(new Language(array('id' => 'hu')));

    $entity = $this->entityStorage->create(array('id' => 'foo'));
    $this->assertSame('hu', $entity->language()->getId());
  }

  /**
   * @covers ::create
   * @covers ::doCreate
   */
  public function testCreateWithExplicitLanguage() {
    $this->languageManager->expects($this->any())
      ->method('getLanguage')
      ->with('en')
      ->willReturn(new Language(array('id' => 'en')));

    $entity = $this->entityStorage->create(array('id' => 'foo', 'langcode' => 'en'));
    $this->assertSame('en', $entity->language()->getId());
  }

269
  /**
270 271
   * @covers ::save
   * @covers ::doSave
272 273 274 275 276 277 278 279 280 281 282 283 284 285
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *
   * @depends testCreate
   */
  public function testSaveInsert(EntityInterface $entity) {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(TRUE));
286 287
    $config_object->expects($this->exactly(1))
      ->method('setData');
288 289
    $config_object->expects($this->once())
      ->method('save');
290 291 292
    $config_object->expects($this->atLeastOnce())
      ->method('get')
      ->willReturn([]);
293

294
    $this->cacheTagsInvalidator->expects($this->once())
295 296
      ->method('invalidateTags')
      ->with(array(
297
        $this->entityTypeId . '_list', // List cache tag.
298 299
      ));

300
    $this->configFactory->expects($this->exactly(1))
301 302 303 304
      ->method('get')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));

305 306 307 308 309
    $this->configFactory->expects($this->exactly(1))
      ->method('getEditable')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    $this->moduleHandler->expects($this->at(0))
      ->method('invokeAll')
      ->with('test_entity_type_presave');
    $this->moduleHandler->expects($this->at(1))
      ->method('invokeAll')
      ->with('entity_presave');
    $this->moduleHandler->expects($this->at(2))
      ->method('invokeAll')
      ->with('test_entity_type_insert');
    $this->moduleHandler->expects($this->at(3))
      ->method('invokeAll')
      ->with('entity_insert');

    $this->entityQuery->expects($this->once())
      ->method('condition')
      ->with('uuid', 'bar')
      ->will($this->returnSelf());
    $this->entityQuery->expects($this->once())
      ->method('execute')
      ->will($this->returnValue(array()));

    $return = $this->entityStorage->save($entity);
    $this->assertSame(SAVED_NEW, $return);
    return $entity;
  }

  /**
337 338
   * @covers ::save
   * @covers ::doSave
339 340 341 342 343 344 345 346 347 348 349 350 351 352
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *
   * @depends testSaveInsert
   */
  public function testSaveUpdate(EntityInterface $entity) {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(FALSE));
353 354
    $config_object->expects($this->exactly(1))
      ->method('setData');
355 356
    $config_object->expects($this->once())
      ->method('save');
357 358 359
    $config_object->expects($this->atLeastOnce())
      ->method('get')
      ->willReturn([]);
360

361
    $this->cacheTagsInvalidator->expects($this->once())
362 363
      ->method('invalidateTags')
      ->with(array(
364 365 366
        // List cache tag only; the own cache tag is invalidated by the config
        // system.
        $this->entityTypeId . '_list',
367 368
      ));

369 370 371 372
    $this->configFactory->expects($this->exactly(2))
      ->method('loadMultiple')
      ->with(array('the_config_prefix.foo'))
      ->will($this->returnValue(array()));
373
    $this->configFactory->expects($this->exactly(1))
374 375 376
      ->method('get')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));
377 378 379 380
    $this->configFactory->expects($this->exactly(1))
      ->method('getEditable')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

    $this->moduleHandler->expects($this->at(0))
      ->method('invokeAll')
      ->with('test_entity_type_presave');
    $this->moduleHandler->expects($this->at(1))
      ->method('invokeAll')
      ->with('entity_presave');
    $this->moduleHandler->expects($this->at(2))
      ->method('invokeAll')
      ->with('test_entity_type_update');
    $this->moduleHandler->expects($this->at(3))
      ->method('invokeAll')
      ->with('entity_update');

    $this->entityQuery->expects($this->once())
      ->method('condition')
      ->with('uuid', 'bar')
      ->will($this->returnSelf());
    $this->entityQuery->expects($this->once())
      ->method('execute')
      ->will($this->returnValue(array($entity->id())));

    $return = $this->entityStorage->save($entity);
    $this->assertSame(SAVED_UPDATED, $return);
    return $entity;
  }

  /**
409 410
   * @covers ::save
   * @covers ::doSave
411 412 413 414 415 416 417 418 419 420
   *
   * @depends testSaveInsert
   */
  public function testSaveRename(ConfigEntityInterface $entity) {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(FALSE));
421 422
    $config_object->expects($this->exactly(1))
      ->method('setData');
423 424
    $config_object->expects($this->once())
      ->method('save');
425 426 427
    $config_object->expects($this->atLeastOnce())
      ->method('get')
      ->willReturn([]);
428

429
    $this->cacheTagsInvalidator->expects($this->once())
430 431
      ->method('invalidateTags')
      ->with(array(
432 433 434
        // List cache tag only; the own cache tag is invalidated by the config
        // system.
        $this->entityTypeId . '_list',
435 436
      ));

437 438
    $this->configFactory->expects($this->once())
      ->method('rename')
439 440 441 442
      ->willReturn($this->configFactory);
    $this->configFactory->expects($this->exactly(1))
      ->method('getEditable')
      ->with('the_config_prefix.bar')
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
      ->will($this->returnValue($config_object));
    $this->configFactory->expects($this->exactly(2))
      ->method('loadMultiple')
      ->with(array('the_config_prefix.foo'))
      ->will($this->returnValue(array()));
    $this->configFactory->expects($this->once())
      ->method('get')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));

    // Performing a rename does not change the original ID until saving.
    $this->assertSame('foo', $entity->getOriginalId());
    $entity->set('id', 'bar');
    $this->assertSame('foo', $entity->getOriginalId());

    $this->entityQuery->expects($this->once())
      ->method('condition')
      ->with('uuid', 'bar')
      ->will($this->returnSelf());
    $this->entityQuery->expects($this->once())
      ->method('execute')
      ->will($this->returnValue(array($entity->id())));

    $return = $this->entityStorage->save($entity);
    $this->assertSame(SAVED_UPDATED, $return);
    $this->assertSame('bar', $entity->getOriginalId());
  }

  /**
472
   * @covers ::save
473 474 475 476 477
   *
   * @expectedException \Drupal\Core\Entity\EntityMalformedException
   * @expectedExceptionMessage The entity does not have an ID.
   */
  public function testSaveInvalid() {
478
    $this->cacheTagsInvalidator->expects($this->never())
479 480
      ->method('invalidateTags');

481 482 483 484 485
    $entity = $this->getMockEntity();
    $this->entityStorage->save($entity);
  }

  /**
486 487
   * @covers ::save
   * @covers ::doSave
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
   *
   * @expectedException \Drupal\Core\Entity\EntityStorageException
   */
  public function testSaveDuplicate() {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(FALSE));
    $config_object->expects($this->never())
      ->method('set');
    $config_object->expects($this->never())
      ->method('save');

503
    $this->cacheTagsInvalidator->expects($this->never())
504 505
      ->method('invalidateTags');

506 507 508 509 510 511 512 513 514 515 516 517
    $this->configFactory->expects($this->once())
      ->method('get')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));

    $entity = $this->getMockEntity(array('id' => 'foo'));
    $entity->enforceIsNew();

    $this->entityStorage->save($entity);
  }

  /**
518 519
   * @covers ::save
   * @covers ::doSave
520 521 522 523 524 525 526 527 528 529 530 531 532 533
   *
   * @expectedException \Drupal\Core\Config\ConfigDuplicateUUIDException
   * @expectedExceptionMessage when this UUID is already used for
   */
  public function testSaveMismatch() {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(TRUE));
    $config_object->expects($this->never())
      ->method('save');

534
    $this->cacheTagsInvalidator->expects($this->never())
535 536
      ->method('invalidateTags');

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    $this->configFactory->expects($this->once())
      ->method('get')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));

    $this->entityQuery->expects($this->once())
      ->method('condition')
      ->will($this->returnSelf());
    $this->entityQuery->expects($this->once())
      ->method('execute')
      ->will($this->returnValue(array('baz')));

    $entity = $this->getMockEntity(array('id' => 'foo'));
    $this->entityStorage->save($entity);
  }

  /**
554 555
   * @covers ::save
   * @covers ::doSave
556 557 558 559 560 561 562 563 564 565
   */
  public function testSaveNoMismatch() {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(TRUE));
    $config_object->expects($this->once())
      ->method('save');
566 567 568
    $config_object->expects($this->atLeastOnce())
      ->method('get')
      ->willReturn([]);
569

570
    $this->cacheTagsInvalidator->expects($this->once())
571 572
      ->method('invalidateTags')
      ->with(array(
573
        $this->entityTypeId . '_list', // List cache tag.
574 575
      ));

576 577 578 579 580 581
    $this->configFactory->expects($this->once())
      ->method('get')
      ->with('the_config_prefix.baz')
      ->will($this->returnValue($config_object));
    $this->configFactory->expects($this->once())
      ->method('rename')
582 583 584 585
      ->willReturn($this->configFactory);
    $this->configFactory->expects($this->exactly(1))
      ->method('getEditable')
      ->with('the_config_prefix.foo')
586 587 588 589 590 591 592 593 594 595
      ->will($this->returnValue($config_object));
    $this->entityQuery->expects($this->once())
      ->method('condition')
      ->will($this->returnSelf());
    $this->entityQuery->expects($this->once())
      ->method('execute')
      ->will($this->returnValue(array('baz')));

    $entity = $this->getMockEntity(array('id' => 'foo'));
    $entity->setOriginalId('baz');
596
    $entity->enforceIsNew();
597 598 599 600
    $this->entityStorage->save($entity);
  }

  /**
601 602
   * @covers ::save
   * @covers ::doSave
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
   *
   * @expectedException \Drupal\Core\Config\ConfigDuplicateUUIDException
   * @expectedExceptionMessage when this entity already exists with UUID
   */
  public function testSaveChangedUuid() {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->atLeastOnce())
      ->method('isNew')
      ->will($this->returnValue(FALSE));
    $config_object->expects($this->never())
      ->method('save');
    $config_object->expects($this->exactly(2))
      ->method('get')
      ->will($this->returnValueMap(array(
        array('', array('id' => 'foo')),
        array('id', 'foo'),
      )));
622 623 624 625 626 627 628 629 630 631 632 633
    $config_object->expects($this->exactly(1))
      ->method('getCacheContexts')
      ->willReturn([]);
    $config_object->expects($this->exactly(1))
      ->method('getCacheTags')
      ->willReturn(['config:foo']);
    $config_object->expects($this->exactly(1))
      ->method('getCacheMaxAge')
      ->willReturn(Cache::PERMANENT);
    $config_object->expects($this->exactly(1))
      ->method('getName')
      ->willReturn('foo');
634

635
    $this->cacheTagsInvalidator->expects($this->never())
636 637
      ->method('invalidateTags');

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    $this->configFactory->expects($this->at(1))
      ->method('loadMultiple')
      ->with(array('the_config_prefix.foo'))
      ->will($this->returnValue(array()));
    $this->configFactory->expects($this->at(2))
      ->method('loadMultiple')
      ->with(array('the_config_prefix.foo'))
      ->will($this->returnValue(array($config_object)));
    $this->configFactory->expects($this->once())
      ->method('get')
      ->with('the_config_prefix.foo')
      ->will($this->returnValue($config_object));
    $this->configFactory->expects($this->never())
      ->method('rename')
      ->will($this->returnValue($config_object));

    $this->moduleHandler->expects($this->exactly(2))
      ->method('getImplementations')
      ->will($this->returnValue(array()));

    $this->entityQuery->expects($this->once())
      ->method('condition')
      ->will($this->returnSelf());
    $this->entityQuery->expects($this->once())
      ->method('execute')
      ->will($this->returnValue(array('foo')));

    $entity = $this->getMockEntity(array('id' => 'foo'));

    $entity->set('uuid', 'baz');
    $this->entityStorage->save($entity);
  }

  /**
672 673 674 675
   * @covers ::load
   * @covers ::postLoad
   * @covers ::mapFromStorageRecords
   * @covers ::doLoadMultiple
676 677 678 679 680 681 682 683 684 685 686
   */
  public function testLoad() {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->exactly(2))
      ->method('get')
      ->will($this->returnValueMap(array(
        array('', array('id' => 'foo')),
        array('id', 'foo'),
      )));
687 688 689 690 691 692 693 694 695 696 697 698
    $config_object->expects($this->exactly(1))
      ->method('getCacheContexts')
      ->willReturn([]);
    $config_object->expects($this->exactly(1))
      ->method('getCacheTags')
      ->willReturn(['config:foo']);
    $config_object->expects($this->exactly(1))
      ->method('getCacheMaxAge')
      ->willReturn(Cache::PERMANENT);
    $config_object->expects($this->exactly(1))
      ->method('getName')
      ->willReturn('foo');
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

    $this->configFactory->expects($this->once())
      ->method('loadMultiple')
      ->with(array('the_config_prefix.foo'))
      ->will($this->returnValue(array($config_object)));
    $this->moduleHandler->expects($this->exactly(2))
      ->method('getImplementations')
      ->will($this->returnValue(array()));

    $entity = $this->entityStorage->load('foo');
    $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity);
    $this->assertSame('foo', $entity->id());
  }

  /**
714 715 716 717
   * @covers ::loadMultiple
   * @covers ::postLoad
   * @covers ::mapFromStorageRecords
   * @covers ::doLoadMultiple
718 719 720 721 722 723 724 725 726 727 728
   */
  public function testLoadMultipleAll() {
    $foo_config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $foo_config_object->expects($this->exactly(2))
      ->method('get')
      ->will($this->returnValueMap(array(
        array('', array('id' => 'foo')),
        array('id', 'foo'),
      )));
729 730 731 732 733 734 735 736 737 738 739 740 741
    $foo_config_object->expects($this->exactly(1))
      ->method('getCacheContexts')
      ->willReturn([]);
    $foo_config_object->expects($this->exactly(1))
      ->method('getCacheTags')
      ->willReturn(['config:foo']);
    $foo_config_object->expects($this->exactly(1))
      ->method('getCacheMaxAge')
      ->willReturn(Cache::PERMANENT);
    $foo_config_object->expects($this->exactly(1))
      ->method('getName')
      ->willReturn('foo');

742 743 744 745 746 747 748 749 750
    $bar_config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $bar_config_object->expects($this->exactly(2))
      ->method('get')
      ->will($this->returnValueMap(array(
        array('', array('id' => 'bar')),
        array('id', 'bar'),
      )));
751 752 753 754 755 756 757 758 759 760 761 762
    $bar_config_object->expects($this->exactly(1))
      ->method('getCacheContexts')
      ->willReturn([]);
    $bar_config_object->expects($this->exactly(1))
      ->method('getCacheTags')
      ->willReturn(['config:bar']);
    $bar_config_object->expects($this->exactly(1))
      ->method('getCacheMaxAge')
      ->willReturn(Cache::PERMANENT);
    $bar_config_object->expects($this->exactly(1))
      ->method('getName')
      ->willReturn('foo');
763

764 765 766 767
    $this->configFactory->expects($this->once())
      ->method('listAll')
      ->with('the_config_prefix.')
      ->will($this->returnValue(array('the_config_prefix.foo' , 'the_config_prefix.bar')));
768 769
    $this->configFactory->expects($this->once())
      ->method('loadMultiple')
770
      ->with(array('the_config_prefix.foo' , 'the_config_prefix.bar'))
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
      ->will($this->returnValue(array($foo_config_object, $bar_config_object)));
    $this->moduleHandler->expects($this->exactly(2))
      ->method('getImplementations')
      ->will($this->returnValue(array()));

    $entities = $this->entityStorage->loadMultiple();
    $expected['foo'] = 'foo';
    $expected['bar'] = 'bar';
    foreach ($entities as $id => $entity) {
      $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity);
      $this->assertSame($id, $entity->id());
      $this->assertSame($expected[$id], $entity->id());
    }
  }

  /**
787 788 789 790
   * @covers ::loadMultiple
   * @covers ::postLoad
   * @covers ::mapFromStorageRecords
   * @covers ::doLoadMultiple
791 792 793 794 795 796 797 798 799 800 801
   */
  public function testLoadMultipleIds() {
    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->exactly(2))
      ->method('get')
      ->will($this->returnValueMap(array(
        array('', array('id' => 'foo')),
        array('id', 'foo'),
      )));
802 803 804 805 806 807 808 809 810 811 812 813
    $config_object->expects($this->exactly(1))
      ->method('getCacheContexts')
      ->willReturn([]);
    $config_object->expects($this->exactly(1))
      ->method('getCacheTags')
      ->willReturn(['config:foo']);
    $config_object->expects($this->exactly(1))
      ->method('getCacheMaxAge')
      ->willReturn(Cache::PERMANENT);
    $config_object->expects($this->exactly(1))
      ->method('getName')
      ->willReturn('foo');
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

    $this->configFactory->expects($this->once())
      ->method('loadMultiple')
      ->with(array('the_config_prefix.foo'))
      ->will($this->returnValue(array($config_object)));
    $this->moduleHandler->expects($this->exactly(2))
      ->method('getImplementations')
      ->will($this->returnValue(array()));

    $entities = $this->entityStorage->loadMultiple(array('foo'));
    foreach ($entities as $id => $entity) {
      $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity);
      $this->assertSame($id, $entity->id());
    }
  }

  /**
831
   * @covers ::loadRevision
832 833
   */
  public function testLoadRevision() {
834
    $this->assertSame(NULL, $this->entityStorage->loadRevision(1));
835 836 837
  }

  /**
838
   * @covers ::deleteRevision
839 840
   */
  public function testDeleteRevision() {
841
    $this->cacheTagsInvalidator->expects($this->never())
842 843
      ->method('invalidateTags');

844 845 846 847
    $this->assertSame(NULL, $this->entityStorage->deleteRevision(1));
  }

  /**
848 849
   * @covers ::delete
   * @covers ::doDelete
850 851
   */
  public function testDelete() {
852 853
    // Dependencies are tested in
    // \Drupal\Tests\config\Kernel\ConfigDependencyTest.
854 855 856
    $this->configManager->expects($this->any())
      ->method('getConfigEntitiesToChangeOnDependencyRemoval')
      ->willReturn(['update' => [], 'delete' => [], 'unchanged' => []]);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    $entities = array();
    $configs = array();
    $config_map = array();
    foreach (array('foo', 'bar') as $id) {
      $entity = $this->getMockEntity(array('id' => $id));
      $entities[] = $entity;
      $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
        ->disableOriginalConstructor()
        ->getMock();
      $config_object->expects($this->once())
        ->method('delete');
      $configs[] = $config_object;
      $config_map[] = array("the_config_prefix.$id", $config_object);
    }

872
    $this->cacheTagsInvalidator->expects($this->once())
873 874
      ->method('invalidateTags')
      ->with(array(
875 876 877
        // List cache tag only; the own cache tag is invalidated by the config
        // system.
        $this->entityTypeId . '_list',
878 879
      ));

880
    $this->configFactory->expects($this->exactly(2))
881
      ->method('getEditable')
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
      ->will($this->returnValueMap($config_map));

    $this->moduleHandler->expects($this->at(0))
      ->method('invokeAll')
      ->with('test_entity_type_predelete');
    $this->moduleHandler->expects($this->at(1))
      ->method('invokeAll')
      ->with('entity_predelete');
    $this->moduleHandler->expects($this->at(2))
      ->method('invokeAll')
      ->with('test_entity_type_predelete');
    $this->moduleHandler->expects($this->at(3))
      ->method('invokeAll')
      ->with('entity_predelete');
    $this->moduleHandler->expects($this->at(4))
      ->method('invokeAll')
      ->with('test_entity_type_delete');
    $this->moduleHandler->expects($this->at(5))
      ->method('invokeAll')
      ->with('entity_delete');
    $this->moduleHandler->expects($this->at(6))
      ->method('invokeAll')
      ->with('test_entity_type_delete');
    $this->moduleHandler->expects($this->at(7))
      ->method('invokeAll')
      ->with('entity_delete');

    $this->entityStorage->delete($entities);
  }

  /**
913 914
   * @covers ::delete
   * @covers ::doDelete
915 916 917 918 919 920 921
   */
  public function testDeleteNothing() {
    $this->moduleHandler->expects($this->never())
      ->method($this->anything());
    $this->configFactory->expects($this->never())
      ->method('get');

922
    $this->cacheTagsInvalidator->expects($this->never())
923 924
      ->method('invalidateTags');

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    $this->entityStorage->delete(array());
  }

  /**
   * Creates an entity with specific methods mocked.
   *
   * @param array $values
   *   (optional) Values to pass to the constructor.
   * @param array $methods
   *   (optional) The methods to mock.
   *
   * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  public function getMockEntity(array $values = array(), $methods = array()) {
    return $this->getMockForAbstractClass('Drupal\Core\Config\Entity\ConfigEntityBase', array($values, 'test_entity_type'), '', TRUE, TRUE, TRUE, $methods);
  }

}

944 945 946 947
namespace Drupal\Core\Config\Entity;

if (!defined('SAVED_NEW')) {
  define('SAVED_NEW', 1);
948
}
949 950
if (!defined('SAVED_UPDATED')) {
  define('SAVED_UPDATED', 2);
951
}