You are here

class FacebookTrackingPixelTestHelper in Facebook Tracking Pixel 7

Same name and namespace in other branches
  1. 8 tests/facebook_tracking_pixel.helper.php \facebookTrackingPixel\FacebookTrackingPixelTestHelper

Hierarchy

Expanded class hierarchy of FacebookTrackingPixelTestHelper

File

tests/facebook_tracking_pixel.helper.php, line 10
Contains tests for the Facebook Tracking Pixel module.

Namespace

facebookTrackingPixel
View source
class FacebookTrackingPixelTestHelper {

  /**
   * Enable tracking for all roles.
   */
  public function enable_tracking_all_roles() {

    // Turn on tracking for roles.
    variable_set('facebook_tracking_pixel_visibility_roles', 0);
    variable_set('facebook_tracking_pixel_roles_administrator', 1);
    variable_set('facebook_tracking_pixel_roles_anonymous_user', 1);
    variable_set('facebook_tracking_pixel_roles_authenticated_user', 1);
  }

  /**
   * Enable tracking for only the testing role.
   */
  public function enable_tracking_testing_role() {

    // Inverse the visibility.
    variable_set('facebook_tracking_pixel_visibility_roles', 1);
    variable_set('facebook_tracking_pixel_roles_fb_pixel_tester', 1);
    variable_set('facebook_tracking_pixel_roles_administrator', 0);
    variable_set('facebook_tracking_pixel_roles_anonymous_user', 0);
    variable_set('facebook_tracking_pixel_roles_authenticated_user', 0);
  }

  /**
   * Remove a role from a user.
   *
   * @param $user
   *   User object or user ID.
   * @param $role_name
   *   String value of role to be removed.
   */
  public function remove_role_from_user($user, $role_name) {

    // For convenience, we'll allow user ids as well as full user objects.
    if (is_numeric($user)) {
      $user = user_load($user);
    }

    // Only remove the role if the user already has it.
    $key = array_search($role_name, $user->roles);
    if ($key == TRUE) {

      // Get the rid from the roles table.
      $roles = user_roles(TRUE);
      $rid = array_search($role_name, $roles);
      if ($rid != FALSE) {

        // Make a copy of the roles array, without the deleted one.
        $new_roles = [];
        foreach ($user->roles as $id => $name) {
          if ($id != $rid) {
            $new_roles[$id] = $name;
          }
        }
        user_save($user, [
          'roles' => $new_roles,
        ]);
      }
    }
  }

  /**
   * Delete all variables.
   */
  public function delete_fb_tracking_role_variables() {

    // Delete all variables via an SQL query.
    $prefix = 'facebook_tracking_pixel_roles';
    $result = db_select('variable', 'v')
      ->fields('v')
      ->condition('name', db_like($prefix) . '%', 'LIKE')
      ->execute()
      ->fetchAll();
    foreach ($result as $item) {
      db_delete('variable')
        ->condition('name', $item->name)
        ->execute();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacebookTrackingPixelTestHelper::delete_fb_tracking_role_variables public function Delete all variables.
FacebookTrackingPixelTestHelper::enable_tracking_all_roles public function Enable tracking for all roles.
FacebookTrackingPixelTestHelper::enable_tracking_testing_role public function Enable tracking for only the testing role.
FacebookTrackingPixelTestHelper::remove_role_from_user public function Remove a role from a user.