View source
<?php
namespace Drupal\Tests\flag\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\flag\Traits\FlagCreateTrait;
class LinkTypeAjaxTest extends WebDriverTestBase {
use FlagCreateTrait;
protected $defaultTheme = 'classy';
protected $flagService;
protected $adminUser;
protected $nodeType = 'article';
protected $flag;
protected $node;
public static $modules = [
'flag',
'flag_event_test',
'node',
'user',
];
protected function setUp() {
parent::setUp();
$this->flagService = $this->container
->get('flag');
$this
->drupalCreateContentType([
'type' => $this->nodeType,
]);
$this->adminUser = $this
->createUser([], NULL, TRUE);
$this->flag = $this
->createFlag('node', [], 'ajax_link');
$this->node = $this
->drupalCreateNode([
'type' => $this->nodeType,
]);
}
public function testAjaxLink() {
$auth_user = $this
->drupalCreateUser([
'flag ' . $this->flag
->id(),
'unflag ' . $this->flag
->id(),
]);
$this
->drupalLogin($auth_user);
$this
->drupalGet($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('flag'));
$this
->clickLink($this->flag
->getShortText('flag'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->addressEquals($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('unflag'));
$this
->assertNotNull($this->flagService
->getFlagging($this->flag, $this->node, $auth_user));
$this
->clickLink($this->flag
->getShortText('unflag'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->addressEquals($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('flag'));
$this
->assertNull($this->flagService
->getFlagging($this->flag, $this->node, $auth_user));
$this
->clickLink($this->flag
->getShortText('flag'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->addressEquals($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('unflag'));
$this
->assertNotNull($this->flagService
->getFlagging($this->flag, $this->node, $auth_user));
$this->flagService
->unflag($this->flag, $this->node, $auth_user);
$flag_b = $this
->createFlag();
$this->container
->get('flag')
->flag($flag_b, $this->node, $auth_user);
$this->container
->get('state')
->set('flag_test.react_flag_event', $flag_b
->id());
$this->container
->get('state')
->set('flag_test.react_unflag_event', $flag_b
->id());
$this
->drupalGet($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('flag'));
$this
->clickLink($this->flag
->getShortText('flag'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->addressEquals($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('unflag'));
$this
->assertNotNull($this->flagService
->getFlagging($this->flag, $this->node, $auth_user));
$this
->assertTrue($this->container
->get('state')
->get('flag_test.is_flagged', FALSE));
$this
->clickLink($this->flag
->getShortText('unflag'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->addressEquals($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('flag'));
$this
->assertNull($this->flagService
->getFlagging($this->flag, $this->node, $auth_user));
$this
->assertTrue($this->container
->get('state')
->get('flag_test.is_unflagged', FALSE));
$this
->clickLink($this->flag
->getShortText('flag'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->addressEquals($this->node
->toUrl());
$this
->assertSession()
->linkExists($this->flag
->getShortText('unflag'));
$this
->assertNotNull($this->flagService
->getFlagging($this->flag, $this->node, $auth_user));
}
}