PageViewTest.php in Simple Facebook Pixel 8
File
tests/src/Functional/PageViewTest.php
View source
<?php
namespace Drupal\Tests\simple_facebook_pixel\Functional;
use Drupal\Tests\BrowserTestBase;
class PageViewTest extends BrowserTestBase {
public static $modules = [
'simple_facebook_pixel',
];
protected $defaultTheme = 'stark';
protected $user;
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'administer simple facebook pixel',
]);
$this
->drupalLogin($this->user);
}
public function testFacebookPixelIdMissing() {
$this
->drupalGet('<front>');
$this
->assertSession()
->statusCodeEquals(200);
$pixel_builder = \Drupal::service('simple_facebook_pixel.pixel_builder');
$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());
}
public function testFacebookPixelDisabled() {
$edit['pixel_enabled'] = FALSE;
$edit['pixel_id'] = '789012';
$this
->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The configuration options have been saved.');
$pixel_builder = \Drupal::service('simple_facebook_pixel.pixel_builder');
$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());
}
public function testDisablingForAdminRoutes() {
$edit['pixel_enabled'] = TRUE;
$edit['pixel_id'] = '789012';
$edit['exclude_admin_pages'] = FALSE;
$this
->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The configuration options have been saved.');
$pixel_builder = \Drupal::service('simple_facebook_pixel.pixel_builder');
$this
->assertSession()
->responseContains($pixel_builder
->getPixelScriptCode());
$edit['pixel_enabled'] = TRUE;
$edit['pixel_id'] = '789012';
$edit['exclude_admin_pages'] = TRUE;
$this
->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The configuration options have been saved.');
$this
->assertSession()
->responseNotContains($pixel_builder
->getPixelScriptCode());
}
public function testFacebookPixelEnabledForAllUsers() {
$edit['pixel_enabled'] = TRUE;
$edit['pixel_id'] = '567123';
$this
->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The configuration options have been saved.');
$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()
->responseContains($pixel_builder
->getPixelScriptCode());
$this
->assertSession()
->responseContains($pixel_builder
->getPixelNoScriptCode());
}
public function testMultiFacebookPixelsEnabledForAllUsers() {
$edit['pixel_enabled'] = TRUE;
$edit['pixel_id'] = '567123, 985473';
$this
->drupalPostForm('admin/config/system/simple-facebook-pixel', $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The configuration options have been saved.');
$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("fbq('init', '567123') fbq('init', '985473')");
$this
->assertSession()
->responseContains($pixel_builder
->getPixelNoScriptCode());
$this
->assertSession()
->responseContains('https://www.facebook.com/tr?id=567123&ev=PageView&noscript=1');
$this
->assertSession()
->responseContains('https://www.facebook.com/tr?id=985473&ev=PageView&noscript=1');
$this
->drupalLogout();
$this
->assertSession()
->responseContains($pixel_builder
->getPixelScriptCode());
$this
->assertSession()
->responseContains($pixel_builder
->getPixelNoScriptCode());
}
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.');
$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());
}
}