You are here

facebook_tracking_pixel.test in Facebook Tracking Pixel 7

Same filename and directory in other branches
  1. 8 tests/facebook_tracking_pixel.test

Contains tests for the Facebook Tracking Pixel module.

File

tests/facebook_tracking_pixel.test
View source
<?php

/**
 * @file
 * Contains tests for the Facebook Tracking Pixel module.
 */

/**
 * Test case.
 */
class FacebookTrackingPixelTestCase extends DrupalWebTestCase {

  /**
   * Node alias created during testing.
   *
   * @var string
   */
  protected $testFacebookTrackingPixelNodeAlias = '';

  /**
   * Generic web user.
   *
   * @var array
   */
  protected $webUser = [];

  /**
   * Adminstrative user.
   *
   * @var array
   */
  protected $adminUser = [];

  /**
   * Facebook Basecode database ID number.
   *
   * @var string
   */
  protected $baseCodeID = '';

  /**
   * Test base code FB ID number.
   *
   * @var string
   */
  protected $baseCodeFBID = '';

  /**
   * Test node id number.
   *
   * @var int
   */
  protected $nodeID = 0;

  /**
   * Test node title.
   *
   * @var string
   */
  protected $nodeTitle = '';

  /**
   * {@inheritdoc}
   */
  function setUp() {

    // Call the parent with an array of modules to enable for the test.
    $modules[] = 'facebook_tracking_pixel';
    parent::setUp($modules);

    // Create a test role to test visibility settings for roles.
    if ($testrid = $this
      ->drupalCreateRole([], 'fb pixel tester')) {

      // Create a generic authenticated user.
      $this->webUser = $this
        ->drupalCreateUser([
        'access content',
      ]);
      $edit = [
        'roles' => [
          $testrid => 'fb pixel tester',
        ],
      ];
      user_save($this->webUser, $edit);
    }

    // Create an admin user and log them in.
    $this->adminUser = $this
      ->drupalCreateUser([
      'administer facebook tracking pixels',
      'access content',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
      'administer users',
      'administer permissions',
    ]);
    $this
      ->drupalLogin($this->adminUser);

    // Set the role as admin for the admin user just created.
    $this
      ->drupalPost('user/' . $this->adminUser->uid . '/edit', [
      'roles[3]' => TRUE,
    ], t('Save'));
    $this->baseCodeFBID = '123123123';

    // Test Basecode ID.
    db_insert('facebook_tracking_pixel_base_codes')
      ->fields([
      'base_code_name' => 'Test Basecode',
      'base_code_fbid' => $this->baseCodeFBID,
      'base_code_global' => 1,
      'weight' => 10,
    ])
      ->execute();

    // Retreieve the base id created.
    $this->baseCodeID = db_select('facebook_tracking_pixel_base_codes', 'c')
      ->fields('c', [
      'base_code_id',
    ])
      ->condition('base_code_fbid', $this->baseCodeFBID, '=')
      ->execute()
      ->fetchField();

    // Create a Node with a path alias for testing.
    // Create node to edit.
    $node = $this
      ->drupalCreateNode();
    $this->nodeTitle = $node->title;
    $this
      ->assertTrue(!empty($node->nid), t('A basic page has been created with node ID %nid and title %title and path alias', [
      '%nid' => $node->nid,
      '%title' => $node->title,
    ]), t('FBTrkPx'));

    // Create alias for the node just created above.
    $edit = [];
    $edit['source'] = 'node/' . $node->nid;
    $edit['alias'] = 'nodetest';
    $this
      ->drupalPost('admin/config/search/path/add', $edit, t('Save'));
    $this->testFacebookTrackingPixelNodeAlias = $edit['alias'];
    $this->nodeID = $node->nid;

    // Visit the system path for the node and confirm a cache entry is
    // created.
    cache_clear_all('*', 'cache_path', TRUE);
    $this
      ->drupalGet($edit['source']);
    $this
      ->assertTrue(cache_get($edit['source'], 'cache_path'), 'Cache entry was created.');

    // Visit the alias for the node and confirm a cache entry is created.
    cache_clear_all('*', 'cache_path', TRUE);
    $this
      ->drupalGet($edit['alias']);
    $this
      ->assertTrue(cache_get($edit['source'], 'cache_path'), 'Cache entry was created.');
  }

}

Classes

Namesort descending Description
FacebookTrackingPixelTestCase Test case.