You are here

public function ViewContentEventTest::testForTaxonomyTerms in Simple Facebook Pixel 8

Tests the case when ViewContent is enabled/disabled for taxonomy terms.

File

tests/src/Functional/ViewContentEventTest.php, line 162

Class

ViewContentEventTest
Tests View Content event.

Namespace

Drupal\Tests\simple_facebook_pixel\Functional

Code

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();

  // Create Tags vocabulary and two test terms.
  $tags_vocabulary = $this
    ->createVocabulary();
  $this
    ->createTerm($tags_vocabulary, [
    'vid' => 'tags',
    'name' => 'Test term #1',
  ]);
  $this
    ->createTerm($tags_vocabulary, [
    'vid' => 'tags',
    'name' => 'Test term #2',
  ]);

  // Make sure that View Content is tracked.
  $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"]});');

  // Create Categories vocabulary and two test terms.
  $categories_vocabulary = $this
    ->createVocabulary();
  $this
    ->createTerm($categories_vocabulary, [
    'vid' => 'categories',
    'name' => 'Test term #3',
  ]);
  $this
    ->createTerm($categories_vocabulary, [
    'vid' => 'categories',
    'name' => 'Test term #4',
  ]);

  // Make sure that View Content is tracked.
  $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"]});');

  // Disable View Content event.
  $this->configFactory
    ->getEditable('simple_facebook_pixel.settings')
    ->set('view_content_entities', [])
    ->save();

  // Make sure that View Content is not tracked.
  $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"]});');

  // Enable View Content event, but just for Tags.
  $this->configFactory
    ->getEditable('simple_facebook_pixel.settings')
    ->set('pixel_id', '1234567890')
    ->set('view_content_entities.taxonomy_term:tags', 'taxonomy_term:tags')
    ->save();

  // Make sure that View Content is tracked for Tags, but not for Categories.
  $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"]});');

  // Log out and test again.
  $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"]});');

  // Disable View Content event.
  $this->configFactory
    ->getEditable('simple_facebook_pixel.settings')
    ->set('view_content_entities', [])
    ->save();

  // Make sure that View Content is not tracked.
  $this
    ->drupalGet('/taxonomy/term/1');
  $this
    ->assertSession()
    ->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test term #1","content_type":"tags","content_ids":["1"]});');
}