You are here

public function LinkTypeAjaxTest::testAjaxLink in Flag 8.4

Test the ajax link type.

File

tests/src/FunctionalJavascript/LinkTypeAjaxTest.php, line 84

Class

LinkTypeAjaxTest
Javascript test for ajax links.

Namespace

Drupal\Tests\flag\FunctionalJavascript

Code

public function testAjaxLink() {

  // Create and login as an authenticated user.
  $auth_user = $this
    ->drupalCreateUser([
    'flag ' . $this->flag
      ->id(),
    'unflag ' . $this->flag
      ->id(),
  ]);
  $this
    ->drupalLogin($auth_user);

  // Navigate to the node page.
  $this
    ->drupalGet($this->node
    ->toUrl());

  // Confirm the flag link exists.
  $this
    ->assertSession()
    ->linkExists($this->flag
    ->getShortText('flag'));

  // Click the flag link.
  $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));

  // Click the unflag link, repeat the check.
  $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));

  // And flag again.
  $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));

  // Add an unrelated flag, and enable flag events.
  // @see \Drupal\flag_test\EventSubscriber\FlagEvents
  $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());

  // Navigate to the node page.
  $this
    ->drupalGet($this->node
    ->toUrl());

  // Confirm the flag link exists.
  $this
    ->assertSession()
    ->linkExists($this->flag
    ->getShortText('flag'));

  // Click the flag link.
  $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));

  // Verifies that the event subscriber was called.
  $this
    ->assertTrue($this->container
    ->get('state')
    ->get('flag_test.is_flagged', FALSE));

  // Click the unflag link, repeat the check.
  $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));

  // Verifies that the event subscriber was called.
  $this
    ->assertTrue($this->container
    ->get('state')
    ->get('flag_test.is_unflagged', FALSE));

  // And flag again.
  $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));
}