You are here

class FacebookTrackingPixelTestCase in Facebook Tracking Pixel 8

Same name and namespace in other branches
  1. 7 tests/facebook_tracking_pixel.test \FacebookTrackingPixelTestCase

Test case.

Hierarchy

Expanded class hierarchy of FacebookTrackingPixelTestCase

File

tests/facebook_tracking_pixel.test, line 11
Contains tests for the Facebook Tracking Pixel module.

View source
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.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacebookTrackingPixelTestCase::$adminUser protected property Adminstrative user.
FacebookTrackingPixelTestCase::$baseCodeFBID protected property Test base code FB ID number.
FacebookTrackingPixelTestCase::$baseCodeID protected property Facebook Basecode database ID number.
FacebookTrackingPixelTestCase::$nodeID protected property Test node id number.
FacebookTrackingPixelTestCase::$nodeTitle protected property Test node title.
FacebookTrackingPixelTestCase::$testFacebookTrackingPixelNodeAlias protected property Node alias created during testing.
FacebookTrackingPixelTestCase::$webUser protected property Generic web user.
FacebookTrackingPixelTestCase::setUp function 1