You are here

public function ShowOnEntityFormTest::testEntityForm in Flag 8.4

Tests if flags appear on the entity form.

File

tests/src/Functional/ShowOnEntityFormTest.php, line 78

Class

ShowOnEntityFormTest
Tests for the base entity Flag Type plugin.

Namespace

Drupal\Tests\flag\Functional

Code

public function testEntityForm() {

  // Login as the admin user.
  $this
    ->drupalLogin($this->adminUser);

  // Create the flag with show_on_form, and grant permissions.
  $edit = [
    'bundles' => [
      $this->nodeType,
    ],
    'flagTypeConfig' => [
      'show_as_field' => TRUE,
      'show_on_form' => TRUE,
      'show_contextual_link' => FALSE,
    ],
  ];
  $flag = $this
    ->createFlagFromArray($edit);
  $this
    ->grantFlagPermissions($flag);
  $flag_checkbox_id = 'edit-flag-' . $flag
    ->id();

  // Create a node and get the ID.
  $node = $this
    ->createNode([
    'type' => $this->nodeType,
  ]);
  $node_id = $node
    ->id();
  $node_edit_path = 'node/' . $node_id . '/edit';

  // See if the form element exists.
  $this
    ->drupalGet($node_edit_path);
  $this
    ->assertSession()
    ->fieldExists($flag_checkbox_id);

  // See if flagging on the form works.
  $edit = [
    'flag[' . $flag
      ->id() . ']' => TRUE,
  ];
  $this
    ->drupalPostForm($node_edit_path, $edit, 'Save');

  // Check to see if the checkbox reflects the state correctly.
  $this
    ->drupalGet($node_edit_path);
  $this
    ->assertSession()
    ->fieldExists($flag_checkbox_id);

  // See if unflagging on the form works.
  $edit = [
    'flag[' . $flag
      ->id() . ']' => FALSE,
  ];
  $this
    ->drupalPostForm($node_edit_path, $edit, 'Save');

  // Go back to the node edit page and check if the flag checkbox is updated.
  $this
    ->drupalGet($node_edit_path);
  $this
    ->assertNoFieldChecked($flag_checkbox_id, 'The flag checkbox is unchecked on the entity form.');

  // Verify link is on the add form.
  $this
    ->drupalGet('node/add/' . $this->nodeType);
  $this
    ->assertSession()
    ->fieldExists($flag_checkbox_id);

  // Tests flagging via the add form.
  $edit = [
    'title[0][value]' => $this
      ->randomString(),
    'flag[' . $flag
      ->id() . ']' => TRUE,
  ];
  $this
    ->drupalPostForm('node/add/' . $this->nodeType, $edit, 'Save');
  $node = $this
    ->getNodeByTitle($edit['title[0][value]']);
  $this
    ->assertTrue($flag
    ->isFlagged($node, $this->adminUser));

  // Tests submitting a new node and not flagging.
  $edit = [
    'title[0][value]' => $this
      ->randomString(),
    'flag[' . $flag
      ->id() . ']' => FALSE,
  ];
  $this
    ->drupalPostForm('node/add/' . $this->nodeType, $edit, 'Save');
  $node = $this
    ->getNodeByTitle($edit['title[0][value]']);
  $this
    ->assertFalse($flag
    ->isFlagged($node, $this->adminUser));

  // Form element should not appear on the delete form.
  $this
    ->drupalGet($node
    ->toUrl('delete-form'));
  $this
    ->assertNoField($flag_checkbox_id);
}