You are here

public function PageViewTest::testFacebookPixelExclusionForRoles in Simple Facebook Pixel 8

Tests snippet exclusion for selected roles.

File

tests/src/Functional/PageViewTest.php, line 159

Class

PageViewTest
Tests PageView event.

Namespace

Drupal\Tests\simple_facebook_pixel\Functional

Code

public function testFacebookPixelExclusionForRoles() {
  $edit['pixel_enabled'] = TRUE;
  $edit['pixel_id'] = '567123';
  $edit['excluded_roles[anonymous]'] = TRUE;
  $edit['excluded_roles[authenticated]'] = FALSE;
  $this
    ->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
  $this
    ->assertSession()
    ->responseContains('The configuration options have been saved.');

  /** @var \Drupal\simple_facebook_pixel\PixelBuilderServiceInterface $pixel_builder */
  $pixel_builder = \Drupal::service('simple_facebook_pixel.pixel_builder');
  $this
    ->drupalGet('<front>');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogout();
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogin($this->user);
  $edit['pixel_enabled'] = TRUE;
  $edit['pixel_id'] = '567123';
  $edit['excluded_roles[anonymous]'] = FALSE;
  $edit['excluded_roles[authenticated]'] = TRUE;
  $this
    ->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
  $this
    ->assertSession()
    ->responseContains('The configuration options have been saved.');
  $this
    ->drupalGet('<front>');
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogout();
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogin($this->user);
  $edit['pixel_enabled'] = TRUE;
  $edit['pixel_id'] = '567123';
  $edit['excluded_roles[anonymous]'] = TRUE;
  $edit['excluded_roles[authenticated]'] = TRUE;
  $this
    ->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
  $this
    ->assertSession()
    ->responseContains('The configuration options have been saved.');
  $this
    ->drupalGet('<front>');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogout();
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseNotContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogin($this->user);
  $edit['pixel_enabled'] = TRUE;
  $edit['pixel_id'] = '567123';
  $edit['excluded_roles[anonymous]'] = FALSE;
  $edit['excluded_roles[authenticated]'] = FALSE;
  $this
    ->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
  $this
    ->assertSession()
    ->responseContains('The configuration options have been saved.');
  $this
    ->drupalGet('<front>');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelNoScriptCode());
  $this
    ->drupalLogout();
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelScriptCode());
  $this
    ->assertSession()
    ->responseContains($pixel_builder
    ->getPixelNoScriptCode());
}