View source
<?php
namespace Drupal\Tests\flag\Functional;
use Drupal\Tests\flag\Traits\FlagCreateTrait;
use Drupal\Tests\flag\Traits\FlagPermissionsTrait;
use Drupal\Tests\BrowserTestBase;
class ShowOnEntityFormTest extends BrowserTestBase {
use FlagCreateTrait;
use FlagPermissionsTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'system',
'user',
'node',
'flag',
];
protected $flag;
protected $adminUser;
protected $nodeType = 'article';
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => $this->nodeType,
]);
$this->adminUser = $this
->drupalCreateUser([
'administer flags',
'administer modules',
'administer nodes',
'create ' . $this->nodeType . ' content',
'edit any ' . $this->nodeType . ' content',
'delete any ' . $this->nodeType . ' content',
]);
}
public function testEntityForm() {
$this
->drupalLogin($this->adminUser);
$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();
$node = $this
->createNode([
'type' => $this->nodeType,
]);
$node_id = $node
->id();
$node_edit_path = 'node/' . $node_id . '/edit';
$this
->drupalGet($node_edit_path);
$this
->assertSession()
->fieldExists($flag_checkbox_id);
$edit = [
'flag[' . $flag
->id() . ']' => TRUE,
];
$this
->drupalPostForm($node_edit_path, $edit, 'Save');
$this
->drupalGet($node_edit_path);
$this
->assertSession()
->fieldExists($flag_checkbox_id);
$edit = [
'flag[' . $flag
->id() . ']' => FALSE,
];
$this
->drupalPostForm($node_edit_path, $edit, 'Save');
$this
->drupalGet($node_edit_path);
$this
->assertNoFieldChecked($flag_checkbox_id, 'The flag checkbox is unchecked on the entity form.');
$this
->drupalGet('node/add/' . $this->nodeType);
$this
->assertSession()
->fieldExists($flag_checkbox_id);
$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));
$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));
$this
->drupalGet($node
->toUrl('delete-form'));
$this
->assertNoField($flag_checkbox_id);
}
}