You are here

public function ViewContentEventTest::testForNodes in Simple Facebook Pixel 8

Tests the case when ViewContent is enabled/disabled for nodes.

File

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

Class

ViewContentEventTest
Tests View Content event.

Namespace

Drupal\Tests\simple_facebook_pixel\Functional

Code

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

  // Create Article content type and two test nodes.
  $this
    ->createContentType([
    'type' => 'article',
  ]);
  $this
    ->createNode([
    'title' => 'Test article #1',
    'type' => 'article',
  ]);
  $this
    ->createNode([
    'title' => 'Test article #2',
    'type' => 'article',
  ]);

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

  // Create Page content type and two test nodes.
  $this
    ->createContentType([
    'type' => 'page',
  ]);
  $this
    ->createNode([
    'title' => 'Test page #3',
    'type' => 'page',
  ]);
  $this
    ->createNode([
    'title' => 'Test page #4',
    'type' => 'page',
  ]);

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

  // 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('/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"]});');

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

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

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

  // 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('/node/1');
  $this
    ->assertSession()
    ->responseNotContains('fbq("track", "ViewContent", {"content_name":"Test article #1","content_type":"article","content_ids":["1"]});');
}