You are here

class FacebookTrackingPixelTestCaseCommerce in Facebook Tracking Pixel 8

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

Test case.

Hierarchy

Expanded class hierarchy of FacebookTrackingPixelTestCaseCommerce

File

tests/facebook_tracking_pixel.commerce.test, line 13

View source
class FacebookTrackingPixelTestCaseCommerce extends CommerceBaseTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return [
      'name' => 'Commerce Test',
      'description' => 'Test the Drupal Commerce features.',
      'group' => 'Facebook Tracking Pixel',
    ];
  }

  /**
   * Administrative user.
   * @var object
   */
  protected $admin_user;

  /**
   * Dummy customer account.
   * @var object
   */
  protected $store_customer;

  /**
   * Dummy product type.
   * @var object
   */
  protected $product_type;

  /**
   * Dummy product.
   * @var object
   */
  protected $product;

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

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

  /**
   * Test SKU number.
   * @var int
   */
  protected $product_sku = 555;

  /**
   * Test product display node id.
   * @var string
   */
  protected $product_node_nid = '';

  /**
   * Testing product name.
   * @var string
   */
  protected $product_name = 'FB Test Product';

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

    // Setup the modules needed for testing.
    $modules = parent::setUpHelper('all', [
      'facebook_tracking_pixel',
    ]);
    parent::setUp($modules);

    // Enable all roles for tracking.
    $fbHelper = new facebookTrackingPixel\FacebookTrackingPixelTestHelper();
    $fbHelper
      ->enable_tracking_all_roles();
    $permissions = array_merge($this
      ->permissionBuilder('store admin'), [
      'administer facebook tracking pixels',
      'access content',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
      'administer users',
      'administer permissions',
    ]);
    $this->admin_user = $this
      ->drupalCreateUser($permissions);
    $this->store_customer = $this
      ->createStoreCustomer();

    // Create a Dummy product type for testing.
    $this->product_type = $this
      ->createDummyProductType();

    // Create a Dummy product using the product type just created with SKU 555.
    $this->product = $this
      ->createDummyProduct($this->product_sku, $this->product_name, -1, 'USD', 1, $this->product_type['type']);

    // Create a product display content type and attach a product field to it.
    $this
      ->createDummyProductDisplayContentType();

    // Create a product display node using the testing SKU.
    $node = $this
      ->createDummyProductNode([
      $this->product->product_id,
    ], 'Test Product Page');
    $this->product_node_nid = $node->nid;

    // Just in case, we clear all caches.
    cache_clear_all();

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

  /**
   * Test tracking of items added to the cart.
   */
  public function testCommerceBaseTrackingAddtoCart() {
    $this
      ->drupalLogin($this->admin_user);
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/commercetracking');
    $edit['facebook_tracking_pixel_commerce_tracking_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_commerce_tracking_enable');
    $edit['facebook_tracking_pixel_commerce_tracking_options_selection[addtocart]'] = TRUE;
    $edit['facebook_tracking_pixel_commerce_tracking_basecode'] = $this->baseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('Commerce Tracking Settings Saved.'), t('Commerce checkout start settings saved.'), 'FBTrkPx');

    // Order must be created via the UI and not programatically.
    // Access the product display node.
    $this
      ->drupalGet('node/' . $this->product_node_nid);
    $edit = [];
    $this
      ->drupalPost(NULL, $edit, t('Add to cart'));
    $this
      ->assertText(t($this->product_name . ' added to your cart'), t('Product added to cart successfully.'));
    $this
      ->assertRaw('facebook_tracking_pixel/commercetracking/fb_trk_addtocart_' . $this->baseCodeFBID . '.js', t('Add to cart tracking code JS found in head.'), 'FBTrkPx');
  }

  /**
   * Test tracking based on starting checkout.
   */
  public function testCommerceBaseTrackingCheckoutStart() {
    $this
      ->drupalLogin($this->admin_user);
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/commercetracking');
    $edit['facebook_tracking_pixel_commerce_tracking_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_commerce_tracking_enable');
    $edit['facebook_tracking_pixel_commerce_tracking_options_selection[checkoutstart]'] = TRUE;
    $edit['facebook_tracking_pixel_commerce_tracking_basecode'] = $this->baseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('Commerce Tracking Settings Saved.'), t('Commerce checkout start settings saved.'), 'FBTrkPx');

    // Order must be created via the UI and not programatically.
    // Access the product display node.
    $this
      ->drupalGet('node/' . $this->product_node_nid);

    // Add product to the shopping cart.
    $edit = [];
    $this
      ->drupalPost(NULL, $edit, t('Add to cart'));
    $this
      ->assertText(t($this->product_name . ' added to your cart'), t('Product added to cart successfully.'));
    $this
      ->drupalGet('checkout');
    $this
      ->assertRaw('facebook_tracking_pixel/commercetracking/fb_trk_checkoutstart_' . $this->baseCodeFBID . '.js', t('Checkout start tracking code JS found in head.'), 'FBTrkPx');
  }

  /**
   * Test completing a purchase tracking.
   */
  public function testCommerceBaseTrackingPurchase() {
    $this
      ->drupalLogin($this->admin_user);
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/commercetracking');
    $edit['facebook_tracking_pixel_commerce_tracking_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_commerce_tracking_enable');
    $edit['facebook_tracking_pixel_commerce_tracking_options_selection[purchase]'] = TRUE;
    $edit['facebook_tracking_pixel_commerce_tracking_basecode'] = $this->baseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('Commerce Tracking Settings Saved.'), t('Commerce checkout start settings saved.'), 'FBTrkPx');

    // Order must be created via the UI and not programatically.
    // Access the product display node.
    $this
      ->drupalGet('node/' . $this->product_node_nid);

    // Add product to the shopping cart.
    $edit = [];
    $this
      ->drupalPost(NULL, $edit, t('Add to cart'));
    $this
      ->assertText(t($this->product_name . ' added to your cart'), t('Product added to cart successfully.'));

    // Go to checkout.
    $this
      ->drupalGet('checkout');

    // Fill out the address form and submit.
    $edit = [];

    // Using a US address because of familiarity.
    $edit['customer_profile_billing[commerce_customer_address][und][0][country]'] = 'US';
    $this
      ->drupalPostAJAX(NULL, $edit, 'customer_profile_billing[commerce_customer_address][und][0][country]');
    $edit['customer_profile_billing[commerce_customer_address][und][0][name_line]'] = $this
      ->randomString();
    $edit['customer_profile_billing[commerce_customer_address][und][0][thoroughfare]'] = '123 Main Street';
    $edit['customer_profile_billing[commerce_customer_address][und][0][locality]'] = 'Smallville';
    $edit['customer_profile_billing[commerce_customer_address][und][0][administrative_area]'] = 'AK';
    $edit['customer_profile_billing[commerce_customer_address][und][0][postal_code]'] = '12345';
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText(t('Review order'), t('Arrived on review order page.'));
    $this
      ->drupalPost(NULL, [], t('Continue to next step'));
    $this
      ->assertRaw('fbq(\'init\', \'' . $this->baseCodeFBID, t('Checkout tracking code JS found in head.'), 'FBTrkPx');
    $this
      ->assertRaw('fbq(\'track\', \'Purchase\'', t('Purchase complete JS found'), 'FBTrkPx');
  }

  /**
   * Test to make sure nothing fires when no options are selected.
   */
  public function testCommerceBaseTrackingNoOptions() {
    $this
      ->drupalLogin($this->admin_user);
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/commercetracking');
    $edit['facebook_tracking_pixel_commerce_tracking_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_commerce_tracking_enable');
    $edit['facebook_tracking_pixel_commerce_tracking_basecode'] = $this->baseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('Commerce Tracking Settings Saved.'), t('Commerce checkout start settings saved.'), 'FBTrkPx');

    // Order must be created via the UI and not programatically.
    // Access the product display node.
    $this
      ->drupalGet('node/' . $this->product_node_nid);

    // Add product to the shopping cart.
    $edit = [];
    $this
      ->drupalPost(NULL, $edit, t('Add to cart'));
    $this
      ->assertText(t($this->product_name . ' added to your cart'), t('Product added to cart successfully.'));

    // Go to checkout.
    $this
      ->drupalGet('checkout');

    // Fill out the address form and submit.
    $edit = [];

    // Using a US address because of familiarity.
    $edit['customer_profile_billing[commerce_customer_address][und][0][country]'] = 'US';
    $this
      ->drupalPostAJAX(NULL, $edit, 'customer_profile_billing[commerce_customer_address][und][0][country]');
    $edit['customer_profile_billing[commerce_customer_address][und][0][name_line]'] = $this
      ->randomString();
    $edit['customer_profile_billing[commerce_customer_address][und][0][thoroughfare]'] = '123 Main Street';
    $edit['customer_profile_billing[commerce_customer_address][und][0][locality]'] = 'Smallville';
    $edit['customer_profile_billing[commerce_customer_address][und][0][administrative_area]'] = 'AK';
    $edit['customer_profile_billing[commerce_customer_address][und][0][postal_code]'] = '12345';
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText(t('Review order'), t('Arrived on review order page.'));
    $this
      ->drupalPost(NULL, [], t('Continue to next step'));
    $this
      ->assertNoRaw('fbq(\'init\', \'' . $this->baseCodeFBID, t('Checkout tracking code JS NOT found in head.'), 'FBTrkPx');
    $this
      ->assertNoRaw('fbq(\'track\', \'Purchase\'', t('Purchase complete JS NOT found'), 'FBTrkPx');
  }

  /**
   * Test disabling tracking with options still selected.
   */
  public function testCommerceBaseTrackingDisable() {
    $this
      ->drupalLogin($this->admin_user);
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/commercetracking');
    $edit['facebook_tracking_pixel_commerce_tracking_enable'] = TRUE;
    $this
      ->drupalPostAJAX(NULL, $edit, 'facebook_tracking_pixel_commerce_tracking_enable');

    // Turn on all tracking.
    $edit['facebook_tracking_pixel_commerce_tracking_options_selection[addtocart]'] = TRUE;
    $edit['facebook_tracking_pixel_commerce_tracking_options_selection[checkoutstart]'] = TRUE;
    $edit['facebook_tracking_pixel_commerce_tracking_options_selection[purchase]'] = TRUE;
    $edit['facebook_tracking_pixel_commerce_tracking_basecode'] = $this->baseCodeID;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->assertText(t('Commerce Tracking Settings Saved.'), t('Commerce checkout start settings saved.'), 'FBTrkPx');

    // Visit the homepage.
    $this
      ->drupalGet('');

    // Return to admin page and disable commerce tracking.
    $edit = [];
    $this
      ->drupalGet('admin/config/system/facebook_tracking_pixel/commercetracking');
    $edit['facebook_tracking_pixel_commerce_tracking_enable'] = FALSE;
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));
    $this
      ->drupalGet('node/' . $this->product_node_nid);
    $edit = [];

    // Add product to cart and make sure event does NOT fire.
    $this
      ->drupalPost(NULL, $edit, t('Add to cart'));
    $this
      ->assertText(t($this->product_name . ' added to your cart'), t('Product added to cart successfully.'));
    $this
      ->assertNoRaw('facebook_tracking_pixel/commercetracking/fb_trk_addtocart_' . $this->baseCodeFBID . '.js', t('Path tracking code JS NOT found in head.'), 'FBTrkPx');

    // Go to checkout page and make sure event does NOT fire.
    $this
      ->drupalGet('checkout');
    $this
      ->assertNoRaw('facebook_tracking_pixel/commercetracking/fb_trk_checkoutstart_' . $this->baseCodeFBID . '.js', t('Checkout start tracking code JS NOT found in head.'), 'FBTrkPx');

    // Fill out the address form and submit.
    $edit = [];

    // Using a US address because of familiarity.
    $edit['customer_profile_billing[commerce_customer_address][und][0][country]'] = 'US';
    $this
      ->drupalPostAJAX(NULL, $edit, 'customer_profile_billing[commerce_customer_address][und][0][country]');
    $edit['customer_profile_billing[commerce_customer_address][und][0][name_line]'] = $this
      ->randomString();
    $edit['customer_profile_billing[commerce_customer_address][und][0][thoroughfare]'] = '123 Main Street';
    $edit['customer_profile_billing[commerce_customer_address][und][0][locality]'] = 'Smallville';
    $edit['customer_profile_billing[commerce_customer_address][und][0][administrative_area]'] = 'AK';
    $edit['customer_profile_billing[commerce_customer_address][und][0][postal_code]'] = '12345';
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText(t('Review order'), t('Arrived on review order page.'));
    $this
      ->drupalPost(NULL, [], t('Continue to next step'));
    $this
      ->assertNoRaw('fbq(\'init\', \'' . $this->baseCodeFBID, t('Checkout tracking code JS NOT found in head.'), 'FBTrkPx');
    $this
      ->assertNoRaw('fbq(\'track\', \'Purchase\'', t('Purchase complete JS NOT found'), 'FBTrkPx');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacebookTrackingPixelTestCaseCommerce::$admin_user protected property Administrative user.
FacebookTrackingPixelTestCaseCommerce::$baseCodeFBID protected property Test base code FB ID number.
FacebookTrackingPixelTestCaseCommerce::$baseCodeID protected property Facebook Basecode database ID number.
FacebookTrackingPixelTestCaseCommerce::$product protected property Dummy product.
FacebookTrackingPixelTestCaseCommerce::$product_name protected property Testing product name.
FacebookTrackingPixelTestCaseCommerce::$product_node_nid protected property Test product display node id.
FacebookTrackingPixelTestCaseCommerce::$product_sku protected property Test SKU number.
FacebookTrackingPixelTestCaseCommerce::$product_type protected property Dummy product type.
FacebookTrackingPixelTestCaseCommerce::$store_customer protected property Dummy customer account.
FacebookTrackingPixelTestCaseCommerce::getInfo public static function
FacebookTrackingPixelTestCaseCommerce::setUp function
FacebookTrackingPixelTestCaseCommerce::testCommerceBaseTrackingAddtoCart public function Test tracking of items added to the cart.
FacebookTrackingPixelTestCaseCommerce::testCommerceBaseTrackingCheckoutStart public function Test tracking based on starting checkout.
FacebookTrackingPixelTestCaseCommerce::testCommerceBaseTrackingDisable public function Test disabling tracking with options still selected.
FacebookTrackingPixelTestCaseCommerce::testCommerceBaseTrackingNoOptions public function Test to make sure nothing fires when no options are selected.
FacebookTrackingPixelTestCaseCommerce::testCommerceBaseTrackingPurchase public function Test completing a purchase tracking.