Skip to content
Snippets Groups Projects
Unverified Commit f3cba0bd authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3513053 by mglaman, phenaproxima: Allow specifying account to switch to...

Issue #3513053 by mglaman, phenaproxima: Allow specifying account to switch to when importing default content
parent bc383a3b
No related branches found
No related tags found
1 merge request!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes
Pipeline #450981 passed with warnings
Pipeline: drupal

#450993

    Pipeline: drupal

    #450987

      Pipeline: drupal

      #450982

        ......@@ -13,6 +13,7 @@
        use Drupal\Core\File\FileSystemInterface;
        use Drupal\Core\Installer\InstallerKernel;
        use Drupal\Core\Language\LanguageManagerInterface;
        use Drupal\Core\Session\AccountInterface;
        use Drupal\file\FileInterface;
        use Drupal\link\Plugin\Field\FieldType\LinkItem;
        use Drupal\user\EntityOwnerInterface;
        ......@@ -61,13 +62,16 @@ public function __construct(
        * - \Drupal\Core\DefaultContent\Existing::Error: Throw an exception.
        * - \Drupal\Core\DefaultContent\Existing::Skip: Leave the existing entity
        * as-is.
        * @param \Drupal\Core\Session\AccountInterface|null $account
        * (optional) The account to use when importing the entities. Defaults to
        * the administrator account.
        *
        * @throws \Drupal\Core\DefaultContent\ImportException
        * - If any of the entities being imported are not content entities.
        * - If any of the entities being imported already exists, by UUID, and
        * $existing is \Drupal\Core\DefaultContent\Existing::Error.
        */
        public function importContent(Finder $content, Existing $existing = Existing::Error): void {
        public function importContent(Finder $content, Existing $existing = Existing::Error, ?AccountInterface $account = NULL): void {
        if (count($content->data) === 0) {
        return;
        }
        ......@@ -75,7 +79,12 @@ public function importContent(Finder $content, Existing $existing = Existing::Er
        $event = new PreImportEvent($content, $existing);
        $skip = $this->eventDispatcher->dispatch($event)->getSkipList();
        $account = $this->accountSwitcher->switchToAdministrator();
        if ($account !== NULL) {
        $this->accountSwitcher->switchTo($account);
        }
        else {
        $account = $this->accountSwitcher->switchToAdministrator();
        }
        try {
        /** @var array{_meta: array<mixed>} $decoded */
        ......
        ......@@ -17,6 +17,7 @@
        use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
        use Drupal\Core\Entity\EntityRepositoryInterface;
        use Drupal\Core\File\FileExists;
        use Drupal\Core\Session\AccountInterface;
        use Drupal\Core\Url;
        use Drupal\field\Entity\FieldConfig;
        use Drupal\field\Entity\FieldStorageConfig;
        ......@@ -33,6 +34,7 @@
        use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
        use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
        use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
        use Drupal\user\UserInterface;
        use Psr\Log\LogLevel;
        use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
        ......@@ -77,12 +79,17 @@ class ContentImportTest extends BrowserTestBase {
        */
        private readonly string $contentDir;
        /**
        * The admin account.
        */
        private UserInterface $adminAccount;
        /**
        * {@inheritdoc}
        */
        protected function setUp(): void {
        parent::setUp();
        $this->setUpCurrentUser(admin: TRUE);
        $this->adminAccount = $this->setUpCurrentUser(admin: TRUE);
        BlockContentType::create(['id' => 'basic', 'label' => 'Basic'])->save();
        block_content_add_body_field('basic');
        ......@@ -162,7 +169,7 @@ public function testDirectContentImport(): void {
        $importer->setLogger($logger);
        $importer->importContent(new Finder($this->contentDir));
        $this->assertContentWasImported();
        $this->assertContentWasImported($this->adminAccount);
        // We should see a warning about importing a file entity associated with a
        // file that doesn't exist.
        $predicate = function (array $record): bool {
        ......@@ -175,6 +182,16 @@ public function testDirectContentImport(): void {
        $this->assertTrue($logger->hasRecordThatPasses($predicate, LogLevel::WARNING));
        }
        /**
        * Tests importing content directly, via the API, with a different user.
        */
        public function testDirectContentImportWithDifferentUser(): void {
        $user = $this->createUser();
        $importer = $this->container->get(Importer::class);
        $importer->importContent(new Finder($this->contentDir), account: $user);
        $this->assertContentWasImported($user);
        }
        /**
        * Tests that the importer validates entities before saving them.
        */
        ......@@ -196,8 +213,11 @@ public function testEntityValidationIsTriggered(): void {
        /**
        * Asserts that the default content was imported as expected.
        *
        * @param \Drupal\Core\Session\AccountInterface $account
        * The account that should own the imported content.
        */
        private function assertContentWasImported(): void {
        private function assertContentWasImported(AccountInterface $account): void {
        /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
        $entity_repository = $this->container->get(EntityRepositoryInterface::class);
        ......@@ -264,10 +284,10 @@ private function assertContentWasImported(): void {
        $this->assertSame("I'd love to put some useful info here.", $block_content->body->value);
        // A node with a non-existent owner should be reassigned to the current
        // user.
        // user or the user provided to the importer.
        $node = $entity_repository->loadEntityByUuid('node', '7f1dd75a-0be2-4d3b-be5d-9d1a868b9267');
        $this->assertInstanceOf(NodeInterface::class, $node);
        $this->assertSame(\Drupal::currentUser()->id(), $node->getOwner()->id());
        $this->assertSame($account->id(), $node->getOwner()->id());
        // Ensure a node with a translation is imported properly.
        $node = $entity_repository->loadEntityByUuid('node', '2d3581c3-92c7-4600-8991-a0d4b3741198');
        ......
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Finish editing this message first!
        Please register or to comment