You are here

public function NodeEditTest::testNodeFormWithOverridePremiumArticlePermission in Node Option Premium 8

Tests with an editor that may only set premium for articles.

File

tests/src/Functional/NodeEditTest.php, line 88

Class

NodeEditTest
Tests editing nodes.

Namespace

Drupal\Tests\nopremium\Functional

Code

public function testNodeFormWithOverridePremiumArticlePermission() {

  // Create a second node type.
  $this
    ->drupalCreateContentType([
    'type' => 'foo',
    'name' => 'Foo',
  ]);

  // Create a user who may create/edit articles and foo nodes, but may only
  // set premium for articles.
  $editor = $this
    ->drupalCreateUser([
    'create article content',
    'edit any article content',
    'create foo content',
    'edit any foo content',
    'override article premium content',
  ]);
  $this
    ->drupalLogin($editor);

  // Create an article and override premium setting.
  $edit = [
    'title[0][value]' => 'Lorem ipsum',
    'premium[value]' => 1,
  ];
  $this
    ->drupalPostForm('node/add/article', $edit, 'Save');

  // Assert that a node was created and premium is enabled.
  $node = Node::load(1);
  $this
    ->assertEquals(1, $node->premium->value);

  // Edit an existing article and enable premium there.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $this
    ->assertEquals(0, $node->premium->value);
  $edit = [
    'premium[value]' => 1,
  ];
  $this
    ->drupalPostForm(sprintf('node/%s/edit', $node
    ->id()), $edit, 'Save');

  // Assert that the node is now premium.
  $node = $this
    ->reloadEntity($node);
  $this
    ->assertEquals(1, $node->premium->value);

  // Also check if this user may *not* edit the premium setting for other node
  // types. Go to the node create page for the foo content type and assert
  // that there's no premium field there.
  $this
    ->drupalGet('node/add/foo');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldNotExists('premium[value]');

  // Check also for an existing foo node.
  $node = $this
    ->drupalCreateNode([
    'type' => 'foo',
  ]);
  $this
    ->drupalGet(sprintf('node/%s/edit', $node
    ->id()));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('Edit Foo');
  $this
    ->assertSession()
    ->fieldNotExists('premium[value]');
}