View source
<?php
namespace Drupal\Tests\simple_facebook_pixel\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class ViewContentEventTest extends BrowserTestBase {
use TaxonomyTestTrait;
public static $modules = [
'node',
'taxonomy',
'simple_facebook_pixel',
];
protected $defaultTheme = 'stark';
protected $user;
protected $configFactory;
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'administer simple facebook pixel',
]);
$this
->drupalLogin($this->user);
$this->configFactory = \Drupal::configFactory();
}
public function testNotEnabled() {
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('pixel_id', '1234567890')
->save();
$this
->drupalGet('<front>');
$this
->assertSession()
->responseNotContains('ViewContent');
}
public function testForNodes() {
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('pixel_enabled', TRUE)
->set('pixel_id', '1234567890')
->set('view_content_entities.node:article', 'node:article')
->set('view_content_entities.node:page', 'node:page')
->save();
$this
->createContentType([
'type' => 'article',
]);
$this
->createNode([
'title' => 'Test article #1',
'type' => 'article',
]);
$this
->createNode([
'title' => 'Test article #2',
'type' => 'article',
]);
$this
->drupalGet('/node/1');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test article #1","content_type":"article","content_ids":["1"]});');
$this
->drupalGet('/node/2');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test article #2","content_type":"article","content_ids":["2"]});');
$this
->createContentType([
'type' => 'page',
]);
$this
->createNode([
'title' => 'Test page #3',
'type' => 'page',
]);
$this
->createNode([
'title' => 'Test page #4',
'type' => 'page',
]);
$this
->drupalGet('/node/3');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test page #3","content_type":"page","content_ids":["3"]});');
$this
->drupalGet('/node/4');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test page #4","content_type":"page","content_ids":["4"]});');
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('view_content_entities', [])
->save();
$this
->drupalGet('/node/1');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test article #1","content_type":"article","content_ids":["1"]});');
$this
->drupalGet('/node/2');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test article #2","content_type":"article","content_ids":["2"]});');
$this
->drupalGet('/node/3');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test page #3","content_type":"page","content_ids":["3"]});');
$this
->drupalGet('/node/4');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test page #4","content_type":"page","content_ids":["4"]});');
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('pixel_id', '1234567890')
->set('view_content_entities.node:article', 'node:article')
->save();
$this
->drupalGet('/node/1');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test article #1","content_type":"article","content_ids":["1"]});');
$this
->drupalGet('/node/3');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test page #3","content_type":"page","content_ids":["3"]});');
$this
->drupalLogout();
$this
->drupalGet('/node/1');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test article #1","content_type":"article","content_ids":["1"]});');
$this
->drupalGet('/node/3');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test page #3","content_type":"page","content_ids":["3"]});');
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('view_content_entities', [])
->save();
$this
->drupalGet('/node/1');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test article #1","content_type":"article","content_ids":["1"]});');
}
public function testForTaxonomyTerms() {
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('pixel_enabled', TRUE)
->set('pixel_id', '1234567890')
->set('view_content_entities.taxonomy_term:tags', 'taxonomy_term:tags')
->set('view_content_entities.taxonomy_term:categories', 'taxonomy_term:categories')
->save();
$tags_vocabulary = $this
->createVocabulary();
$this
->createTerm($tags_vocabulary, [
'vid' => 'tags',
'name' => 'Test term #1',
]);
$this
->createTerm($tags_vocabulary, [
'vid' => 'tags',
'name' => 'Test term #2',
]);
$this
->drupalGet('/taxonomy/term/1');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test term #1","content_type":"tags","content_ids":["1"]});');
$this
->drupalGet('/taxonomy/term/2');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test term #2","content_type":"tags","content_ids":["2"]});');
$categories_vocabulary = $this
->createVocabulary();
$this
->createTerm($categories_vocabulary, [
'vid' => 'categories',
'name' => 'Test term #3',
]);
$this
->createTerm($categories_vocabulary, [
'vid' => 'categories',
'name' => 'Test term #4',
]);
$this
->drupalGet('/taxonomy/term/3');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test term #3","content_type":"categories","content_ids":["3"]});');
$this
->drupalGet('/taxonomy/term/4');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test term #4","content_type":"categories","content_ids":["4"]});');
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('view_content_entities', [])
->save();
$this
->drupalGet('/taxonomy/term/1');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #1","content_type":"tags","content_ids":["1"]});');
$this
->drupalGet('/taxonomy/term/2');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #2","content_type":"tags","content_ids":["2"]});');
$this
->drupalGet('/taxonomy/term/3');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #3","content_type":"categories","content_ids":["3"]});');
$this
->drupalGet('/taxonomy/term/4');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #4","content_type":"categories","content_ids":["4"]});');
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('pixel_id', '1234567890')
->set('view_content_entities.taxonomy_term:tags', 'taxonomy_term:tags')
->save();
$this
->drupalGet('/taxonomy/term/1');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test term #1","content_type":"tags","content_ids":["1"]});');
$this
->drupalGet('/taxonomy/term/3');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #3","content_type":"categories","content_ids":["3"]});');
$this
->drupalLogout();
$this
->drupalGet('/taxonomy/term/1');
$this
->assertSession()
->responseContains('fbq("track", "ViewContent", {"content_name":"Test term #1","content_type":"tags","content_ids":["1"]});');
$this
->drupalGet('/taxonomy/term/3');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #3","content_type":"categories","content_ids":["3"]});');
$this->configFactory
->getEditable('simple_facebook_pixel.settings')
->set('view_content_entities', [])
->save();
$this
->drupalGet('/taxonomy/term/1');
$this
->assertSession()
->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #1","content_type":"tags","content_ids":["1"]});');
}
}