View source
<?php
namespace Drupal\Tests\flag\Functional;
class LinkTypeConfirmFormTest extends FlagTestBase {
protected $flagConfirmMessage = 'Flag test label 123?';
protected $unflagConfirmMessage = 'Unflag test label 123?';
protected $createButtonText = 'Create flagging 123?';
protected $deleteButtonText = 'Delete flagging 123?';
protected $flag;
public function testCreateConfirmFlag() {
$this
->drupalLogin($this->adminUser);
$this
->doCreateFlag();
$this
->doFlagUnflagNode();
}
public function doCreateFlag() {
$edit = [
'bundles' => [
$this->nodeType,
],
'linkTypeConfig' => [
'flag_confirmation' => $this->flagConfirmMessage,
'unflag_confirmation' => $this->unflagConfirmMessage,
'flag_create_button' => $this->createButtonText,
'flag_delete_button' => $this->deleteButtonText,
],
'link_type' => 'confirm',
];
$this->flag = $this
->createFlagFromArray($edit);
}
public function doFlagUnflagNode() {
$node = $this
->drupalCreateNode([
'type' => $this->nodeType,
]);
$node_id = $node
->id();
$flag_id = $this->flag
->id();
$this
->grantFlagPermissions($this->flag);
$user_1 = $this
->drupalCreateUser();
$this
->drupalLogin($user_1);
$flag_count_pre = \Drupal::database()
->query('SELECT count FROM {flag_counts}
WHERE flag_id = :flag_id AND entity_type = :entity_type AND entity_id = :entity_id', [
':flag_id' => $flag_id,
':entity_type' => 'node',
':entity_id' => $node_id,
])
->fetchField();
$this
->drupalGet('node/' . $node_id);
$this
->clickLink($this->flag
->getShortText('flag'));
$this
->assertSession()
->pageTextContains($this->flagConfirmMessage);
$this
->drupalPostForm('flag/confirm/flag/' . $flag_id . '/' . $node_id, [], $this->createButtonText);
$this
->drupalGet('node/' . $node_id);
$this
->assertLink($this->flag
->getShortText('unflag'));
$flag_count_flagged = \Drupal::database()
->query('SELECT count FROM {flag_counts}
WHERE flag_id = :flag_id AND entity_type = :entity_type AND entity_id = :entity_id', [
':flag_id' => $flag_id,
':entity_type' => 'node',
':entity_id' => $node_id,
])
->fetchField();
$this
->assertEqual($flag_count_flagged, $flag_count_pre + 1, "The flag count was incremented.");
$this
->clickLink($this->flag
->getShortText('unflag'));
$this
->assertSession()
->pageTextContains($this->unflagConfirmMessage);
$this
->drupalPostForm(NULL, [], $this->deleteButtonText);
$this
->drupalGet('node/' . $node_id);
$this
->assertLink($this->flag
->getShortText('flag'));
$flag_count_unflagged = \Drupal::database()
->query('SELECT count FROM {flag_counts}
WHERE flag_id = :flag_id AND entity_type = :entity_type AND entity_id = :entity_id', [
':flag_id' => $flag_id,
':entity_type' => 'node',
':entity_id' => $node_id,
])
->fetchField();
$this
->assertEqual($flag_count_unflagged, $flag_count_flagged - 1, "The flag count was decremented.");
}
}