You are here

facebook_tracking_pixel.user.test in Facebook Tracking Pixel 8

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

Contains tests for the Facebook Tracking Pixel module.

File

tests/facebook_tracking_pixel.user.test
View source
<?php

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

/**
 * Test case.
 */
require_once 'facebook_tracking_pixel.helper.php';
class FacebookTrackingPixelTestCaseUser extends FacebookTrackingPixelTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return [
      'name' => 'User Registration Test',
      'description' => 'Test user registration tracking.',
      'group' => 'Facebook Tracking Pixel',
    ];
  }

  /**
   * Basecode ID number for user testing.
   *
   * @var int
   */
  public $userBaseCodeID;

  /**
   * Fictional FBID for user testing.
   * @var string
   */
  public $userBaseCodeFBID = '345345345345';

  /**
   * {@inheritdoc}
   */
  function setUp() {
    parent::setUp();

    // Enable all roles for tracking.
    $fbHelper = new facebookTrackingPixel\FacebookTrackingPixelTestHelper();
    $fbHelper
      ->enable_tracking_all_roles();

    // Create a non-global base code to test with user tracking.
    // Test Basecode ID.
    db_insert('facebook_tracking_pixel_base_codes')
      ->fields([
      'base_code_name' => 'Test Basecode 2',
      'base_code_fbid' => $this->userBaseCodeFBID,
      'base_code_global' => 0,
      'weight' => 20,
    ])
      ->execute();

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

  /**
   * Test to enable user tracking on global base code.
   */
  public function testFacebookTrackingPixelUserRegistrationGlobal() {

    // Turn on user registration and select test global base code.
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/user_registration');
    $edit['facebook_tracking_pixel_user_registration_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_user_registration_enable');
    $edit['facebook_tracking_pixel_user_registration_basecode'] = $this->baseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('User tracking settings saved.'), t('User registration tracking enabled.'), 'FBTrkPx');

    // Logout and drop into creating a normal user to test user registration.
    $this
      ->drupalLogout();
    variable_set('user_email_verification', 0);
    variable_set('user_register', 1);
    $this
      ->drupalGet('user/register');
    $edit = [];
    $edit['name'] = $this
      ->randomName(8);
    $edit['mail'] = $edit['name'] . '@example.com';
    $pass = user_password(12);
    $edit['pass[pass1]'] = $pass;
    $edit['pass[pass2]'] = $pass;
    $this
      ->drupalPost(NULL, $edit, t('Create new account'));
    $this
      ->assertText(t('Registration successful. You are now logged in.'), t('Created a user using the UI and logged in.'), 'FBTrkPx');
    $this
      ->assertRaw('facebook_tracking_pixel/registrationtracking/fb_trk_user_registration.js', t('Path tracking code JS found in head.'), 'FBTrkPx');
  }

  /**
   * Test tracking on a non-global tracking code.
   */
  public function testFacebookTrackingPixelUserRegistrationNonGlobal() {

    // Turn on user registration and select test global base code.
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/user_registration');
    $edit['facebook_tracking_pixel_user_registration_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_user_registration_enable');
    $edit['facebook_tracking_pixel_user_registration_basecode'] = $this->userBaseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('User tracking settings saved.'), t('User registration tracking enabled.'), 'FBTrkPx');

    // Logout and drop into creating a normal user to test user registration.
    $this
      ->drupalLogout();
    variable_set('user_email_verification', 0);
    variable_set('user_register', 1);
    $this
      ->drupalGet('user/register');
    $edit = [];
    $edit['name'] = $this
      ->randomName(8);
    $edit['mail'] = $edit['name'] . '@example.com';
    $pass = user_password(12);
    $edit['pass[pass1]'] = $pass;
    $edit['pass[pass2]'] = $pass;
    $this
      ->drupalPost(NULL, $edit, t('Create new account'));
    $this
      ->assertText(t('Registration successful. You are now logged in.'), t('Created a user using the UI and logged in.'), 'FBTrkPx');
    $this
      ->assertRaw('facebook_tracking_pixel/registrationtracking/fb_trk_user_registration.js', t('Path tracking code JS found in head.'), 'FBTrkPx');
  }

}