You are here

protected function BulkFormTest::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/Views/BulkFormTest.php \Drupal\Tests\node\Functional\Views\BulkFormTest::setUp()
  2. 9 core/modules/node/tests/src/Functional/Views/BulkFormTest.php \Drupal\Tests\node\Functional\Views\BulkFormTest::setUp()

Overrides NodeTestBase::setUp

File

core/modules/node/tests/src/Functional/Views/BulkFormTest.php, line 45

Class

BulkFormTest
Tests a node bulk form.

Namespace

Drupal\Tests\node\Functional\Views

Code

protected function setUp($import_test_views = TRUE, $modules = [
  'node_test_views',
]) : void {
  parent::setUp($import_test_views, $modules);
  ConfigurableLanguage::createFromLangcode('en-gb')
    ->save();
  ConfigurableLanguage::createFromLangcode('it')
    ->save();

  // Create some test nodes.
  $this->nodes = [];
  $langcodes = [
    'en',
    'en-gb',
    'it',
  ];
  for ($i = 1; $i <= 5; $i++) {
    $langcode = $langcodes[($i - 1) % 3];
    $values = [
      'title' => $this
        ->randomMachineName() . ' [' . $i . ':' . $langcode . ']',
      'langcode' => $langcode,
      'promote' => FALSE,
    ];
    $node = $this
      ->drupalCreateNode($values);
    $this->nodes[] = $node;
  }

  // Create translations for all languages for some nodes.
  for ($i = 0; $i < 2; $i++) {
    $node = $this->nodes[$i];
    foreach ($langcodes as $langcode) {
      if (!$node
        ->hasTranslation($langcode)) {
        $title = $this
          ->randomMachineName() . ' [' . $node
          ->id() . ':' . $langcode . ']';
        $translation = $node
          ->addTranslation($langcode, [
          'title' => $title,
          'promote' => FALSE,
        ]);
      }
    }
    $node
      ->save();
  }

  // Create a node with only one translation.
  $node = $this->nodes[2];
  $langcode = 'en';
  $title = $this
    ->randomMachineName() . ' [' . $node
    ->id() . ':' . $langcode . ']';
  $translation = $node
    ->addTranslation($langcode, [
    'title' => $title,
  ]);
  $node
    ->save();

  // Check that all created translations are selected by the test view.
  $view = Views::getView('test_node_bulk_form');
  $view
    ->execute();
  $this
    ->assertCount(10, $view->result, 'All created translations are selected.');

  // Check the operations are accessible to the logged in user.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'administer nodes',
    'access content overview',
    'bypass node access',
  ]));
  $this
    ->drupalGet('test-node-bulk-form');
  $elements = $this
    ->assertSession()
    ->selectExists('edit-action')
    ->findAll('css', 'option');
  $this
    ->assertCount(8, $elements, 'All node operations are found.');
}