Same name in other branches
  1. 8.x-1.x tests/src/Functional/EntityCloneContentRecursiveTest.php \Drupal\Tests\entity_clone\Functional\EntityCloneContentRecursiveTest

Create a content and test a clone.

@group entity_clone

Hierarchy

  • class \Drupal\Tests\entity_clone\Functional\EntityCloneContentRecursiveTest extends \Drupal\Tests\node\Functional\NodeTestBase uses \Drupal\Core\StringTranslation\StringTranslationTrait

Expanded class hierarchy of EntityCloneContentRecursiveTest

File

tests/src/Functional/EntityCloneContentRecursiveTest.php, line 15

Namespace

Drupal\Tests\entity_clone\Functional
View source
class EntityCloneContentRecursiveTest extends NodeTestBase {
    use StringTranslationTrait;
    
    /**
     * Modules to enable.
     *
     * @var array
     */
    protected static $modules = [
        'entity_clone',
        'block',
        'node',
        'datetime',
    ];
    
    /**
     * Theme to enable by default.
     *
     * @var string
     */
    protected $defaultTheme = 'claro';
    
    /**
     * Profile to install.
     *
     * @var string
     */
    protected $profile = 'standard';
    
    /**
     * Permissions to grant admin user.
     *
     * @var array
     */
    protected $permissions = [
        'bypass node access',
        'administer nodes',
        'clone node entity',
    ];
    
    /**
     * A user with permission to bypass content access checks.
     *
     * @var \Drupal\user\UserInterface
     */
    protected $adminUser;
    
    /**
     * Sets the test up.
     */
    protected function setUp() : void {
        parent::setUp();
        $this->adminUser = $this->drupalCreateUser($this->permissions);
        $this->drupalLogin($this->adminUser);
    }
    
    /**
     * Test clone a content entity with another entities attached.
     */
    public function testContentEntityClone() {
        $term_title = $this->randomMachineName(8);
        $term = Term::create([
            'vid' => 'tags',
            'name' => $term_title,
        ]);
        $term->save();
        $node_title = $this->randomMachineName(8);
        $node = Node::create([
            'type' => 'article',
            'title' => $node_title,
            'field_tags' => [
                'target_id' => $term->id(),
            ],
        ]);
        $node->save();
        $settings = [
            'taxonomy_term' => [
                'default_value' => 1,
                'disable' => 0,
                'hidden' => 0,
            ],
        ];
        \Drupal::service('config.factory')->getEditable('entity_clone.settings')
            ->set('form_settings', $settings)
            ->save();
        $this->drupalGet('entity_clone/node/' . $node->id());
        $this->submitForm([
            'recursive[node.article.field_tags][references][' . $term->id() . '][clone]' => 1,
        ], $this->t('Clone'));
        $nodes = \Drupal::entityTypeManager()->getStorage('node')
            ->loadByProperties([
            'title' => $node_title . ' - Cloned',
        ]);
        
        /** @var \Drupal\node\Entity\Node $node */
        $node = reset($nodes);
        $this->assertInstanceOf(Node::class, $node, 'Test node cloned found in database.');
        $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
            ->loadByProperties([
            'name' => $term_title . ' - Cloned',
        ]);
        
        /** @var \Drupal\taxonomy\Entity\Term $term */
        $term = reset($terms);
        $this->assertInstanceOf(Term::class, $term, 'Test term referenced by node cloned too found in database.');
        $node->delete();
        $term->delete();
        $nodes = \Drupal::entityTypeManager()->getStorage('node')
            ->loadByProperties([
            'title' => $node_title,
        ]);
        $node = reset($nodes);
        $this->assertInstanceOf(Node::class, $node, 'Test original node found in database.');
        $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
            ->loadByProperties([
            'name' => $term_title,
        ]);
        $term = reset($terms);
        $this->assertInstanceOf(Term::class, $term, 'Test original term found in database.');
    }

}

Members

Title Sort descending Modifiers Object type Summary
EntityCloneContentRecursiveTest::$adminUser protected property A user with permission to bypass content access checks.
EntityCloneContentRecursiveTest::$defaultTheme protected property Theme to enable by default.
EntityCloneContentRecursiveTest::$modules protected static property Modules to enable.
EntityCloneContentRecursiveTest::$permissions protected property Permissions to grant admin user.
EntityCloneContentRecursiveTest::$profile protected property Profile to install.
EntityCloneContentRecursiveTest::setUp protected function Sets the test up.
EntityCloneContentRecursiveTest::testContentEntityClone public function Test clone a content entity with another entities attached.