You are here

protected function NodeAccessLanguageAwareCombinationTest::setUp in Drupal 9

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

Overrides NodeAccessTestBase::setUp

File

core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareCombinationTest.php, line 53

Class

NodeAccessLanguageAwareCombinationTest
Tests node access functionality with multiple languages and two node access modules.

Namespace

Drupal\Tests\node\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  node_access_test_add_field(NodeType::load('page'));

  // Create the 'private' field, which allows the node to be marked as private
  // (restricted access) in a given translation.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_private',
    'entity_type' => 'node',
    'type' => 'boolean',
    'cardinality' => 1,
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'page',
    'widget' => [
      'type' => 'options_buttons',
    ],
    'settings' => [
      'on_label' => 'Private',
      'off_label' => 'Not private',
    ],
  ])
    ->save();

  // After enabling a node access module, the access table has to be rebuild.
  node_access_rebuild();

  // Add Hungarian and Catalan.
  ConfigurableLanguage::createFromLangcode('hu')
    ->save();
  ConfigurableLanguage::createFromLangcode('ca')
    ->save();

  // Create a normal authenticated user.
  $this->webUser = $this
    ->drupalCreateUser([
    'access content',
  ]);

  // Load the user 1 user for later use as an admin user with permission to
  // see everything.
  $this->adminUser = User::load(1);

  // The node_access_test_language module allows individual translations of a
  // node to be marked private (not viewable by normal users), and the
  // node_access_test module allows whole nodes to be marked private. (In a
  // real-world implementation, hook_node_access_records_alter() might be
  // implemented by one or both modules to enforce that private nodes or
  // translations are always private, but we want to test the default,
  // additive behavior of node access).
  // Create six Hungarian nodes with Catalan translations:
  // 1. One public with neither language marked as private.
  // 2. One private with neither language marked as private.
  // 3. One public with only the Hungarian translation private.
  // 4. One public with only the Catalan translation private.
  // 5. One public with both the Hungarian and Catalan translations private.
  // 6. One private with both the Hungarian and Catalan translations private.
  $this->nodes['public_both_public'] = $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'langcode' => 'hu',
    'field_private' => [
      [
        'value' => 0,
      ],
    ],
    'private' => FALSE,
  ]);
  $translation = $node
    ->addTranslation('ca');
  $translation->title->value = $this
    ->randomString();
  $translation->field_private->value = 0;
  $node
    ->save();
  $this->nodes['private_both_public'] = $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'langcode' => 'hu',
    'field_private' => [
      [
        'value' => 0,
      ],
    ],
    'private' => TRUE,
  ]);
  $translation = $node
    ->addTranslation('ca');
  $translation->title->value = $this
    ->randomString();
  $translation->field_private->value = 0;
  $node
    ->save();
  $this->nodes['public_hu_private'] = $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'langcode' => 'hu',
    'field_private' => [
      [
        'value' => 1,
      ],
    ],
    'private' => FALSE,
  ]);
  $translation = $node
    ->addTranslation('ca');
  $translation->title->value = $this
    ->randomString();
  $translation->field_private->value = 0;
  $node
    ->save();
  $this->nodes['public_ca_private'] = $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'langcode' => 'hu',
    'field_private' => [
      [
        'value' => 0,
      ],
    ],
    'private' => FALSE,
  ]);
  $translation = $node
    ->addTranslation('ca');
  $translation->title->value = $this
    ->randomString();
  $translation->field_private->value = 1;
  $node
    ->save();
  $this->nodes['public_both_private'] = $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'langcode' => 'hu',
    'field_private' => [
      [
        'value' => 1,
      ],
    ],
    'private' => FALSE,
  ]);
  $translation = $node
    ->addTranslation('ca');
  $translation->title->value = $this
    ->randomString();
  $translation->field_private->value = 1;
  $node
    ->save();
  $this->nodes['private_both_private'] = $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'langcode' => 'hu',
    'field_private' => [
      [
        'value' => 1,
      ],
    ],
    'private' => TRUE,
  ]);
  $translation = $node
    ->addTranslation('ca');
  $translation->title->value = $this
    ->randomString();
  $translation->field_private->value = 1;
  $node
    ->save();
  $this->nodes['public_no_language_private'] = $this
    ->drupalCreateNode([
    'field_private' => [
      [
        'value' => 1,
      ],
    ],
    'private' => FALSE,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $this->nodes['public_no_language_public'] = $this
    ->drupalCreateNode([
    'field_private' => [
      [
        'value' => 0,
      ],
    ],
    'private' => FALSE,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $this->nodes['private_no_language_private'] = $this
    ->drupalCreateNode([
    'field_private' => [
      [
        'value' => 1,
      ],
    ],
    'private' => TRUE,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $this->nodes['private_no_language_public'] = $this
    ->drupalCreateNode([
    'field_private' => [
      [
        'value' => 1,
      ],
    ],
    'private' => TRUE,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
}