1<?php 2 3namespace Drupal\Tests\Core\Entity\KeyValueStore; 4 5use Drupal\Core\Cache\MemoryCache\MemoryCache; 6use Drupal\Core\Config\Entity\ConfigEntityInterface; 7use Drupal\Core\DependencyInjection\ContainerBuilder; 8use Drupal\Core\Entity\EntityFieldManagerInterface; 9use Drupal\Core\Entity\EntityInterface; 10use Drupal\Core\Entity\EntityMalformedException; 11use Drupal\Core\Entity\EntityStorageException; 12use Drupal\Core\Entity\EntityTypeManagerInterface; 13use Drupal\Core\Language\Language; 14use Drupal\Tests\UnitTestCase; 15use Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage; 16 17/** 18 * @coversDefaultClass \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage 19 * @group Entity 20 */ 21class KeyValueEntityStorageTest extends UnitTestCase { 22 23 /** 24 * The entity type. 25 * 26 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject 27 */ 28 protected $entityType; 29 30 /** 31 * The key value store. 32 * 33 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface|\PHPUnit\Framework\MockObject\MockObject 34 */ 35 protected $keyValueStore; 36 37 /** 38 * The module handler. 39 * 40 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject 41 */ 42 protected $moduleHandler; 43 44 /** 45 * The UUID service. 46 * 47 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\MockObject 48 */ 49 protected $uuidService; 50 51 /** 52 * The language manager. 53 * 54 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject 55 */ 56 protected $languageManager; 57 58 /** 59 * @var \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage 60 */ 61 protected $entityStorage; 62 63 /** 64 * The mocked entity type manager. 65 * 66 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject 67 */ 68 protected $entityTypeManager; 69 70 /** 71 * The mocked entity field manager. 72 * 73 * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit\Framework\MockObject\MockObject 74 */ 75 protected $entityFieldManager; 76 77 /** 78 * The mocked cache tags invalidator. 79 * 80 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit\Framework\MockObject\MockObject 81 */ 82 protected $cacheTagsInvalidator; 83 84 /** 85 * {@inheritdoc} 86 */ 87 protected function setUp() { 88 parent::setUp(); 89 $this->entityType = $this->createMock('Drupal\Core\Entity\EntityTypeInterface'); 90 } 91 92 /** 93 * Prepares the key value entity storage. 94 * 95 * @covers ::__construct 96 * 97 * @param string $uuid_key 98 * (optional) The entity key used for the UUID. Defaults to 'uuid'. 99 */ 100 protected function setUpKeyValueEntityStorage($uuid_key = 'uuid') { 101 $this->entityType->expects($this->atLeastOnce()) 102 ->method('getKey') 103 ->will($this->returnValueMap([ 104 ['id', 'id'], 105 ['uuid', $uuid_key], 106 ['langcode', 'langcode'], 107 ])); 108 $this->entityType->expects($this->atLeastOnce()) 109 ->method('id') 110 ->will($this->returnValue('test_entity_type')); 111 $this->entityType->expects($this->any()) 112 ->method('getListCacheTags') 113 ->willReturn(['test_entity_type_list']); 114 115 $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class); 116 $this->entityTypeManager->expects($this->any()) 117 ->method('getDefinition') 118 ->with('test_entity_type') 119 ->will($this->returnValue($this->entityType)); 120 121 $this->entityFieldManager = $this->createMock(EntityFieldManagerInterface::class); 122 123 $this->cacheTagsInvalidator = $this->createMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); 124 125 $this->keyValueStore = $this->createMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface'); 126 $this->moduleHandler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface'); 127 $this->uuidService = $this->createMock('Drupal\Component\Uuid\UuidInterface'); 128 $this->languageManager = $this->createMock('Drupal\Core\Language\LanguageManagerInterface'); 129 $language = new Language(['langcode' => 'en']); 130 $this->languageManager->expects($this->any()) 131 ->method('getDefaultLanguage') 132 ->will($this->returnValue($language)); 133 $this->languageManager->expects($this->any()) 134 ->method('getCurrentLanguage') 135 ->will($this->returnValue($language)); 136 137 $this->entityStorage = new KeyValueEntityStorage($this->entityType, $this->keyValueStore, $this->uuidService, $this->languageManager, new MemoryCache()); 138 $this->entityStorage->setModuleHandler($this->moduleHandler); 139 140 $container = new ContainerBuilder(); 141 $container->set('entity_field.manager', $this->entityFieldManager); 142 $container->set('entity_type.manager', $this->entityTypeManager); 143 $container->set('language_manager', $this->languageManager); 144 $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); 145 \Drupal::setContainer($container); 146 } 147 148 /** 149 * @covers ::create 150 * @covers ::doCreate 151 */ 152 public function testCreateWithPredefinedUuid() { 153 $this->entityType->expects($this->once()) 154 ->method('getClass') 155 ->will($this->returnValue(get_class($this->getMockEntity()))); 156 $this->setUpKeyValueEntityStorage(); 157 158 $this->moduleHandler->expects($this->at(0)) 159 ->method('invokeAll') 160 ->with('test_entity_type_create'); 161 $this->moduleHandler->expects($this->at(1)) 162 ->method('invokeAll') 163 ->with('entity_create'); 164 $this->uuidService->expects($this->never()) 165 ->method('generate'); 166 167 $entity = $this->entityStorage->create(['id' => 'foo', 'uuid' => 'baz']); 168 $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); 169 $this->assertSame('foo', $entity->id()); 170 $this->assertSame('baz', $entity->uuid()); 171 } 172 173 /** 174 * @covers ::create 175 * @covers ::doCreate 176 */ 177 public function testCreateWithoutUuidKey() { 178 // Set up the entity storage to expect no UUID key. 179 $this->entityType->expects($this->once()) 180 ->method('getClass') 181 ->will($this->returnValue(get_class($this->getMockEntity()))); 182 $this->setUpKeyValueEntityStorage(NULL); 183 184 $this->moduleHandler->expects($this->at(0)) 185 ->method('invokeAll') 186 ->with('test_entity_type_create'); 187 $this->moduleHandler->expects($this->at(1)) 188 ->method('invokeAll') 189 ->with('entity_create'); 190 $this->uuidService->expects($this->never()) 191 ->method('generate'); 192 193 $entity = $this->entityStorage->create(['id' => 'foo', 'uuid' => 'baz']); 194 $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); 195 $this->assertSame('foo', $entity->id()); 196 $this->assertSame('baz', $entity->uuid()); 197 } 198 199 /** 200 * @covers ::create 201 * @covers ::doCreate 202 * 203 * @return \Drupal\Core\Entity\EntityInterface 204 */ 205 public function testCreate() { 206 $entity = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [], ['toArray']); 207 $this->entityType->expects($this->once()) 208 ->method('getClass') 209 ->will($this->returnValue(get_class($entity))); 210 $this->setUpKeyValueEntityStorage(); 211 212 $this->moduleHandler->expects($this->at(0)) 213 ->method('invokeAll') 214 ->with('test_entity_type_create'); 215 $this->moduleHandler->expects($this->at(1)) 216 ->method('invokeAll') 217 ->with('entity_create'); 218 $this->uuidService->expects($this->once()) 219 ->method('generate') 220 ->will($this->returnValue('bar')); 221 222 $entity = $this->entityStorage->create(['id' => 'foo']); 223 $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); 224 $this->assertSame('foo', $entity->id()); 225 $this->assertSame('bar', $entity->uuid()); 226 return $entity; 227 } 228 229 /** 230 * @covers ::save 231 * @covers ::doSave 232 * 233 * @param \Drupal\Core\Entity\EntityInterface $entity 234 * 235 * @return \Drupal\Core\Entity\EntityInterface 236 * 237 * @depends testCreate 238 */ 239 public function testSaveInsert(EntityInterface $entity) { 240 $this->entityType->expects($this->once()) 241 ->method('getClass') 242 ->will($this->returnValue(get_class($entity))); 243 $this->setUpKeyValueEntityStorage(); 244 245 $expected = ['id' => 'foo']; 246 $this->keyValueStore->expects($this->exactly(2)) 247 ->method('has') 248 ->with('foo') 249 ->will($this->returnValue(FALSE)); 250 $this->keyValueStore->expects($this->never()) 251 ->method('getMultiple'); 252 $this->keyValueStore->expects($this->never()) 253 ->method('delete'); 254 255 $entity->expects($this->atLeastOnce()) 256 ->method('toArray') 257 ->will($this->returnValue($expected)); 258 259 $this->moduleHandler->expects($this->at(0)) 260 ->method('invokeAll') 261 ->with('test_entity_type_presave'); 262 $this->moduleHandler->expects($this->at(1)) 263 ->method('invokeAll') 264 ->with('entity_presave'); 265 $this->moduleHandler->expects($this->at(2)) 266 ->method('invokeAll') 267 ->with('test_entity_type_insert'); 268 $this->moduleHandler->expects($this->at(3)) 269 ->method('invokeAll') 270 ->with('entity_insert'); 271 $this->keyValueStore->expects($this->once()) 272 ->method('set') 273 ->with('foo', $expected); 274 $return = $this->entityStorage->save($entity); 275 $this->assertSame(SAVED_NEW, $return); 276 return $entity; 277 } 278 279 /** 280 * @covers ::save 281 * @covers ::doSave 282 * 283 * @param \Drupal\Core\Entity\EntityInterface $entity 284 * 285 * @return \Drupal\Core\Entity\EntityInterface 286 * 287 * @depends testSaveInsert 288 */ 289 public function testSaveUpdate(EntityInterface $entity) { 290 $this->entityType->expects($this->once()) 291 ->method('getClass') 292 ->will($this->returnValue(get_class($entity))); 293 $this->setUpKeyValueEntityStorage(); 294 295 $expected = ['id' => 'foo']; 296 $this->keyValueStore->expects($this->exactly(2)) 297 ->method('has') 298 ->with('foo') 299 ->will($this->returnValue(TRUE)); 300 $this->keyValueStore->expects($this->once()) 301 ->method('getMultiple') 302 ->with(['foo']) 303 ->will($this->returnValue([['id' => 'foo']])); 304 $this->keyValueStore->expects($this->never()) 305 ->method('delete'); 306 307 $this->moduleHandler->expects($this->at(0)) 308 ->method('getImplementations') 309 ->with('entity_load') 310 ->will($this->returnValue([])); 311 $this->moduleHandler->expects($this->at(1)) 312 ->method('getImplementations') 313 ->with('test_entity_type_load') 314 ->will($this->returnValue([])); 315 $this->moduleHandler->expects($this->at(2)) 316 ->method('invokeAll') 317 ->with('test_entity_type_presave'); 318 $this->moduleHandler->expects($this->at(3)) 319 ->method('invokeAll') 320 ->with('entity_presave'); 321 $this->moduleHandler->expects($this->at(4)) 322 ->method('invokeAll') 323 ->with('test_entity_type_update'); 324 $this->moduleHandler->expects($this->at(5)) 325 ->method('invokeAll') 326 ->with('entity_update'); 327 $this->keyValueStore->expects($this->once()) 328 ->method('set') 329 ->with('foo', $expected); 330 $return = $this->entityStorage->save($entity); 331 $this->assertSame(SAVED_UPDATED, $return); 332 return $entity; 333 } 334 335 /** 336 * @covers ::save 337 * @covers ::doSave 338 */ 339 public function testSaveConfigEntity() { 340 $this->setUpKeyValueEntityStorage(); 341 342 $entity = $this->getMockEntity('Drupal\Core\Config\Entity\ConfigEntityBase', [['id' => 'foo']], [ 343 'toArray', 344 'preSave', 345 ]); 346 $entity->enforceIsNew(); 347 // When creating a new entity, the ID is tracked as the original ID. 348 $this->assertSame('foo', $entity->getOriginalId()); 349 350 $expected = ['id' => 'foo']; 351 $entity->expects($this->atLeastOnce()) 352 ->method('toArray') 353 ->will($this->returnValue($expected)); 354 355 $this->keyValueStore->expects($this->exactly(2)) 356 ->method('has') 357 ->with('foo') 358 ->will($this->returnValue(FALSE)); 359 $this->keyValueStore->expects($this->once()) 360 ->method('set') 361 ->with('foo', $expected); 362 $this->keyValueStore->expects($this->never()) 363 ->method('delete'); 364 365 $return = $this->entityStorage->save($entity); 366 $this->assertSame(SAVED_NEW, $return); 367 return $entity; 368 } 369 370 /** 371 * @covers ::save 372 * @covers ::doSave 373 * 374 * @depends testSaveConfigEntity 375 */ 376 public function testSaveRenameConfigEntity(ConfigEntityInterface $entity) { 377 $this->entityType->expects($this->once()) 378 ->method('getClass') 379 ->will($this->returnValue(get_class($entity))); 380 $this->setUpKeyValueEntityStorage(); 381 382 $this->moduleHandler->expects($this->at(0)) 383 ->method('getImplementations') 384 ->with('entity_load') 385 ->will($this->returnValue([])); 386 $this->moduleHandler->expects($this->at(1)) 387 ->method('getImplementations') 388 ->with('test_entity_type_load') 389 ->will($this->returnValue([])); 390 $expected = ['id' => 'foo']; 391 $entity->expects($this->once()) 392 ->method('toArray') 393 ->will($this->returnValue($expected)); 394 $this->keyValueStore->expects($this->exactly(2)) 395 ->method('has') 396 ->with('foo') 397 ->will($this->returnValue(TRUE)); 398 $this->keyValueStore->expects($this->once()) 399 ->method('getMultiple') 400 ->with(['foo']) 401 ->will($this->returnValue([['id' => 'foo']])); 402 $this->keyValueStore->expects($this->once()) 403 ->method('delete') 404 ->with('foo'); 405 $this->keyValueStore->expects($this->once()) 406 ->method('set') 407 ->with('bar', $expected); 408 409 // Performing a rename does not change the original ID until saving. 410 $this->assertSame('foo', $entity->getOriginalId()); 411 $entity->set('id', 'bar'); 412 $this->assertSame('foo', $entity->getOriginalId()); 413 414 $return = $this->entityStorage->save($entity); 415 $this->assertSame(SAVED_UPDATED, $return); 416 $this->assertSame('bar', $entity->getOriginalId()); 417 } 418 419 /** 420 * @covers ::save 421 * @covers ::doSave 422 */ 423 public function testSaveContentEntity() { 424 $this->entityType->expects($this->any()) 425 ->method('getKeys') 426 ->will($this->returnValue([ 427 'id' => 'id', 428 ])); 429 $this->setUpKeyValueEntityStorage(); 430 431 $expected = ['id' => 'foo']; 432 $this->keyValueStore->expects($this->exactly(2)) 433 ->method('has') 434 ->with('foo') 435 ->will($this->returnValue(FALSE)); 436 $this->keyValueStore->expects($this->once()) 437 ->method('set') 438 ->with('foo', $expected); 439 $this->keyValueStore->expects($this->never()) 440 ->method('delete'); 441 $entity = $this->getMockEntity('Drupal\Core\Entity\ContentEntityBase', [], [ 442 'toArray', 443 'id', 444 ]); 445 $entity->expects($this->atLeastOnce()) 446 ->method('id') 447 ->will($this->returnValue('foo')); 448 $entity->expects($this->once()) 449 ->method('toArray') 450 ->will($this->returnValue($expected)); 451 $this->entityStorage->save($entity); 452 } 453 454 /** 455 * @covers ::save 456 * @covers ::doSave 457 */ 458 public function testSaveInvalid() { 459 $this->setUpKeyValueEntityStorage(); 460 461 $entity = $this->getMockEntity('Drupal\Core\Config\Entity\ConfigEntityBase'); 462 $this->keyValueStore->expects($this->never()) 463 ->method('has'); 464 $this->keyValueStore->expects($this->never()) 465 ->method('set'); 466 $this->keyValueStore->expects($this->never()) 467 ->method('delete'); 468 $this->expectException(EntityMalformedException::class); 469 $this->expectExceptionMessage('The entity does not have an ID.'); 470 $this->entityStorage->save($entity); 471 } 472 473 /** 474 * @covers ::save 475 * @covers ::doSave 476 */ 477 public function testSaveDuplicate() { 478 $this->setUpKeyValueEntityStorage(); 479 480 $entity = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]); 481 $entity->enforceIsNew(); 482 $this->keyValueStore->expects($this->once()) 483 ->method('has') 484 ->will($this->returnValue(TRUE)); 485 $this->keyValueStore->expects($this->never()) 486 ->method('set'); 487 $this->keyValueStore->expects($this->never()) 488 ->method('delete'); 489 $this->expectException(EntityStorageException::class); 490 $this->expectExceptionMessage("'test_entity_type' entity with ID 'foo' already exists"); 491 $this->entityStorage->save($entity); 492 } 493 494 /** 495 * @covers ::load 496 * @covers ::postLoad 497 */ 498 public function testLoad() { 499 $entity = $this->getMockEntity(); 500 $this->entityType->expects($this->once()) 501 ->method('getClass') 502 ->will($this->returnValue(get_class($entity))); 503 $this->setUpKeyValueEntityStorage(); 504 505 $this->keyValueStore->expects($this->once()) 506 ->method('getMultiple') 507 ->with(['foo']) 508 ->will($this->returnValue([['id' => 'foo']])); 509 $this->moduleHandler->expects($this->at(0)) 510 ->method('getImplementations') 511 ->with('entity_load') 512 ->will($this->returnValue([])); 513 $this->moduleHandler->expects($this->at(1)) 514 ->method('getImplementations') 515 ->with('test_entity_type_load') 516 ->will($this->returnValue([])); 517 $entity = $this->entityStorage->load('foo'); 518 $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); 519 $this->assertSame('foo', $entity->id()); 520 } 521 522 /** 523 * @covers ::load 524 */ 525 public function testLoadMissingEntity() { 526 $this->entityType->expects($this->once()) 527 ->method('getClass'); 528 $this->setUpKeyValueEntityStorage(); 529 530 $this->keyValueStore->expects($this->once()) 531 ->method('getMultiple') 532 ->with(['foo']) 533 ->will($this->returnValue([])); 534 $this->moduleHandler->expects($this->never()) 535 ->method('getImplementations'); 536 $entity = $this->entityStorage->load('foo'); 537 $this->assertNull($entity); 538 } 539 540 /** 541 * @covers ::loadMultiple 542 * @covers ::postLoad 543 * @covers ::mapFromStorageRecords 544 * @covers ::doLoadMultiple 545 */ 546 public function testLoadMultipleAll() { 547 $expected['foo'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]); 548 $expected['bar'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'bar']]); 549 $this->entityType->expects($this->once()) 550 ->method('getClass') 551 ->will($this->returnValue(get_class(reset($expected)))); 552 $this->setUpKeyValueEntityStorage(); 553 554 $this->keyValueStore->expects($this->once()) 555 ->method('getAll') 556 ->will($this->returnValue([['id' => 'foo'], ['id' => 'bar']])); 557 $this->moduleHandler->expects($this->at(0)) 558 ->method('getImplementations') 559 ->with('entity_load') 560 ->will($this->returnValue([])); 561 $this->moduleHandler->expects($this->at(1)) 562 ->method('getImplementations') 563 ->with('test_entity_type_load') 564 ->will($this->returnValue([])); 565 $entities = $this->entityStorage->loadMultiple(); 566 foreach ($entities as $id => $entity) { 567 $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); 568 $this->assertSame($id, $entity->id()); 569 $this->assertSame($id, $expected[$id]->id()); 570 } 571 } 572 573 /** 574 * @covers ::loadMultiple 575 * @covers ::postLoad 576 * @covers ::mapFromStorageRecords 577 * @covers ::doLoadMultiple 578 */ 579 public function testLoadMultipleIds() { 580 $entity = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]); 581 $this->entityType->expects($this->once()) 582 ->method('getClass') 583 ->will($this->returnValue(get_class($entity))); 584 $this->setUpKeyValueEntityStorage(); 585 586 $expected[] = $entity; 587 $this->keyValueStore->expects($this->once()) 588 ->method('getMultiple') 589 ->with(['foo']) 590 ->will($this->returnValue([['id' => 'foo']])); 591 $this->moduleHandler->expects($this->at(0)) 592 ->method('getImplementations') 593 ->with('entity_load') 594 ->will($this->returnValue([])); 595 $this->moduleHandler->expects($this->at(1)) 596 ->method('getImplementations') 597 ->with('test_entity_type_load') 598 ->will($this->returnValue([])); 599 $entities = $this->entityStorage->loadMultiple(['foo']); 600 foreach ($entities as $id => $entity) { 601 $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); 602 $this->assertSame($id, $entity->id()); 603 } 604 } 605 606 /** 607 * @covers ::loadRevision 608 */ 609 public function testLoadRevision() { 610 $this->setUpKeyValueEntityStorage(); 611 612 $this->assertSame(NULL, $this->entityStorage->loadRevision(1)); 613 } 614 615 /** 616 * @covers ::deleteRevision 617 */ 618 public function testDeleteRevision() { 619 $this->setUpKeyValueEntityStorage(); 620 621 $this->assertSame(NULL, $this->entityStorage->deleteRevision(1)); 622 } 623 624 /** 625 * @covers ::delete 626 * @covers ::doDelete 627 */ 628 public function testDelete() { 629 $entities['foo'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]); 630 $entities['bar'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'bar']]); 631 $this->entityType->expects($this->once()) 632 ->method('getClass') 633 ->will($this->returnValue(get_class(reset($entities)))); 634 $this->setUpKeyValueEntityStorage(); 635 636 $this->moduleHandler->expects($this->at(0)) 637 ->method('invokeAll') 638 ->with('test_entity_type_predelete'); 639 $this->moduleHandler->expects($this->at(1)) 640 ->method('invokeAll') 641 ->with('entity_predelete'); 642 $this->moduleHandler->expects($this->at(2)) 643 ->method('invokeAll') 644 ->with('test_entity_type_predelete'); 645 $this->moduleHandler->expects($this->at(3)) 646 ->method('invokeAll') 647 ->with('entity_predelete'); 648 $this->moduleHandler->expects($this->at(4)) 649 ->method('invokeAll') 650 ->with('test_entity_type_delete'); 651 $this->moduleHandler->expects($this->at(5)) 652 ->method('invokeAll') 653 ->with('entity_delete'); 654 $this->moduleHandler->expects($this->at(6)) 655 ->method('invokeAll') 656 ->with('test_entity_type_delete'); 657 $this->moduleHandler->expects($this->at(7)) 658 ->method('invokeAll') 659 ->with('entity_delete'); 660 661 $this->keyValueStore->expects($this->once()) 662 ->method('deleteMultiple') 663 ->with(['foo', 'bar']); 664 $this->entityStorage->delete($entities); 665 } 666 667 /** 668 * @covers ::delete 669 * @covers ::doDelete 670 */ 671 public function testDeleteNothing() { 672 $this->setUpKeyValueEntityStorage(); 673 674 $this->moduleHandler->expects($this->never()) 675 ->method($this->anything()); 676 $this->keyValueStore->expects($this->never()) 677 ->method('delete'); 678 $this->keyValueStore->expects($this->never()) 679 ->method('deleteMultiple'); 680 681 $this->entityStorage->delete([]); 682 } 683 684 /** 685 * Creates an entity with specific methods mocked. 686 * 687 * @param string $class 688 * (optional) The concrete entity class to mock. Defaults to 689 * 'Drupal\Core\Entity\EntityBase'. 690 * @param array $arguments 691 * (optional) Arguments to pass to the constructor. An empty set of values 692 * and an entity type ID will be provided. 693 * @param array $methods 694 * (optional) The methods to mock. 695 * 696 * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject 697 */ 698 public function getMockEntity($class = 'Drupal\Core\Entity\EntityBase', array $arguments = [], $methods = []) { 699 // Ensure the entity is passed at least an array of values and an entity 700 // type ID 701 if (!isset($arguments[0])) { 702 $arguments[0] = []; 703 } 704 if (!isset($arguments[1])) { 705 $arguments[1] = 'test_entity_type'; 706 } 707 return $this->getMockForAbstractClass($class, $arguments, '', TRUE, TRUE, TRUE, $methods); 708 } 709 710} 711 712namespace Drupal\Core\Entity\KeyValueStore; 713 714if (!defined('SAVED_NEW')) { 715 define('SAVED_NEW', 1); 716} 717if (!defined('SAVED_UPDATED')) { 718 define('SAVED_UPDATED', 2); 719} 720