ContentEntityNullStorageTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\contact\Entity\ContactForm;
  4. use Drupal\Core\Config\ConfigImporter;
  5. use Drupal\Core\Config\StorageComparer;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests ContentEntityNullStorage entity query support.
  9. *
  10. * @see \Drupal\Core\Entity\ContentEntityNullStorage
  11. * @see \Drupal\Core\Entity\Query\Null\Query
  12. *
  13. * @group Entity
  14. */
  15. class ContentEntityNullStorageTest extends KernelTestBase {
  16. /**
  17. * Modules to enable.
  18. *
  19. * @var array
  20. */
  21. public static $modules = ['system', 'contact', 'user'];
  22. /**
  23. * Tests using entity query with ContentEntityNullStorage.
  24. *
  25. * @see \Drupal\Core\Entity\Query\Null\Query
  26. */
  27. public function testEntityQuery() {
  28. $this->assertSame(0, \Drupal::entityQuery('contact_message')->count()->execute(), 'Counting a null storage returns 0.');
  29. $this->assertSame([], \Drupal::entityQuery('contact_message')->execute(), 'Querying a null storage returns an empty array.');
  30. $this->assertSame([], \Drupal::entityQuery('contact_message')->condition('contact_form', 'test')->execute(), 'Querying a null storage returns an empty array and conditions are ignored.');
  31. $this->assertSame([], \Drupal::entityQueryAggregate('contact_message')->aggregate('name', 'AVG')->execute(), 'Aggregate querying a null storage returns an empty array');
  32. }
  33. /**
  34. * Tests deleting a contact form entity via a configuration import.
  35. *
  36. * @see \Drupal\Core\Entity\Event\BundleConfigImportValidate
  37. */
  38. public function testDeleteThroughImport() {
  39. $contact_form = ContactForm::create(['id' => 'test']);
  40. $contact_form->save();
  41. $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
  42. // Set up the ConfigImporter object for testing.
  43. $storage_comparer = new StorageComparer(
  44. $this->container->get('config.storage.sync'),
  45. $this->container->get('config.storage'),
  46. $this->container->get('config.manager')
  47. );
  48. $config_importer = new ConfigImporter(
  49. $storage_comparer->createChangelist(),
  50. $this->container->get('event_dispatcher'),
  51. $this->container->get('config.manager'),
  52. $this->container->get('lock'),
  53. $this->container->get('config.typed'),
  54. $this->container->get('module_handler'),
  55. $this->container->get('module_installer'),
  56. $this->container->get('theme_handler'),
  57. $this->container->get('string_translation')
  58. );
  59. // Delete the contact message in sync.
  60. $sync = $this->container->get('config.storage.sync');
  61. $sync->delete($contact_form->getConfigDependencyName());
  62. // Import.
  63. $config_importer->reset()->import();
  64. $this->assertNull(ContactForm::load($contact_form->id()), 'The contact form has been deleted.');
  65. }
  66. }