function DevelGenerateBrowserTest::testDevelGenerateBatchContent

Same name in other branches
  1. 4.x devel_generate/tests/src/Functional/DevelGenerateBrowserTest.php \Drupal\Tests\devel_generate\Functional\DevelGenerateBrowserTest::testDevelGenerateBatchContent()

Tests generating content in batch mode.

File

devel_generate/tests/src/Functional/DevelGenerateBrowserTest.php, line 384

Class

DevelGenerateBrowserTest
Tests the logic to generate data.

Namespace

Drupal\Tests\devel_generate\Functional

Code

public function testDevelGenerateBatchContent() : void {
    // For 50 or more nodes, the processing will be done via batch.
    $edit = [
        'num' => 55,
        'kill' => TRUE,
        'node_types[article]' => TRUE,
        'node_types[page]' => TRUE,
    ];
    $this->drupalGet('admin/config/development/generate/content');
    $this->submitForm($edit, 'Generate');
    $this->assertSession()
        ->pageTextContains('Finished 55 elements created successfully.');
    $this->assertSession()
        ->pageTextContains('Generate process complete.');
    // Tests that the expected number of nodes have been created.
    $count = count(Node::loadMultiple());
    $this->assertEquals(55, $count, sprintf('The expected total number of nodes is %s, found %s', 55, $count));
    // Create nodes with translations via batch.
    $edit = [
        'num' => 52,
        'kill' => TRUE,
        'node_types[article]' => TRUE,
        'node_types[page]' => TRUE,
        'add_language[]' => [
            'en',
        ],
        'translate_language[]' => [
            'de',
            'ca',
        ],
    ];
    $this->drupalGet('admin/config/development/generate/content');
    $this->submitForm($edit, 'Generate');
    $this->assertCount(52, \Drupal::entityQuery('node')->accessCheck(FALSE)
        ->execute());
    // Only articles will have translations so get that number.
    $articles = \Drupal::entityQuery('node')->accessCheck(FALSE)
        ->condition('type', 'article')
        ->execute();
    $this->assertSession()
        ->pageTextContains(sprintf('Finished 52 elements and %s translations created successfully.', 2 * count($articles)));
    // Generate only articles.
    $edit = [
        'num' => 60,
        'kill' => TRUE,
        'node_types[article]' => TRUE,
        'node_types[page]' => FALSE,
    ];
    $this->drupalGet('admin/config/development/generate/content');
    $this->submitForm($edit, 'Generate');
    // Tests that all the created nodes were of the node type selected.
    $nodeStorage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $type = 'article';
    $count = $nodeStorage->getQuery()
        ->condition('type', $type)
        ->accessCheck(FALSE)
        ->count()
        ->execute();
    $this->assertEquals(60, $count, sprintf('The expected number of %s is %s, found %s', $type, 60, $count));
}