You are here

protected function NodeRevisionsTest::setUp in Drupal 9

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

Overrides NodeTestBase::setUp

File

core/modules/node/tests/src/Functional/NodeRevisionsTest.php, line 55

Class

NodeRevisionsTest
Create a node with revisions and test viewing, saving, reverting, and deleting revisions for users with access for this content type.

Namespace

Drupal\Tests\node\Functional

Code

protected function setUp() : void {
  parent::setUp();

  // Enable additional languages.
  ConfigurableLanguage::createFromLangcode('de')
    ->save();
  ConfigurableLanguage::createFromLangcode('it')
    ->save();
  $field_storage_definition = [
    'field_name' => 'untranslatable_string_field',
    'entity_type' => 'node',
    'type' => 'string',
    'cardinality' => 1,
    'translatable' => FALSE,
  ];
  $field_storage = FieldStorageConfig::create($field_storage_definition);
  $field_storage
    ->save();
  $field_definition = [
    'field_storage' => $field_storage,
    'bundle' => 'page',
  ];
  $field = FieldConfig::create($field_definition);
  $field
    ->save();

  // Enable translation for page nodes.
  \Drupal::service('content_translation.manager')
    ->setEnabled('node', 'page', TRUE);

  // Create and log in user.
  $web_user = $this
    ->drupalCreateUser([
    'view page revisions',
    'revert page revisions',
    'delete page revisions',
    'edit any page content',
    'delete any page content',
    'access contextual links',
    'translate any entity',
    'administer content types',
  ]);
  $this
    ->drupalLogin($web_user);

  // Create initial node.
  $node = $this
    ->drupalCreateNode();
  $settings = get_object_vars($node);
  $settings['revision'] = 1;
  $settings['isDefaultRevision'] = TRUE;
  $nodes = [];
  $logs = [];

  // Get original node.
  $nodes[] = clone $node;

  // Create three revisions.
  $revision_count = 3;
  for ($i = 0; $i < $revision_count; $i++) {
    $logs[] = $node->revision_log = $this
      ->randomMachineName(32);

    // Create revision with a random title and body and update variables.
    $node->title = $this
      ->randomMachineName();
    $node->body = [
      'value' => $this
        ->randomMachineName(32),
      'format' => filter_default_format(),
    ];
    $node->untranslatable_string_field->value = $this
      ->randomString();
    $node
      ->setNewRevision();

    // Edit the 1st and 2nd revision with a different user.
    if ($i < 2) {
      $editor = $this
        ->drupalCreateUser();
      $node
        ->setRevisionUserId($editor
        ->id());
    }
    else {
      $node
        ->setRevisionUserId($web_user
        ->id());
    }
    $node
      ->save();

    // Make sure we get revision information.
    $node = Node::load($node
      ->id());
    $nodes[] = clone $node;
  }
  $this->nodes = $nodes;
  $this->revisionLogs = $logs;
}