View source
<?php
require_once 'facebook_tracking_pixel.helper.php';
class FacebookTrackingPixelTestCaseCommerce extends CommerceBaseTestCase {
public static function getInfo() {
return [
'name' => 'Commerce Test',
'description' => 'Test the Drupal Commerce features.',
'group' => 'Facebook Tracking Pixel',
];
}
protected $admin_user;
protected $store_customer;
protected $product_type;
protected $product;
protected $baseCodeID = '';
protected $baseCodeFBID = '789789789';
protected $product_sku = 555;
protected $product_node_nid = '';
protected $product_name = 'FB Test Product';
function setUp() {
$modules = parent::setUpHelper('all', [
'facebook_tracking_pixel',
]);
parent::setUp($modules);
$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();
$this->product_type = $this
->createDummyProductType();
$this->product = $this
->createDummyProduct($this->product_sku, $this->product_name, -1, 'USD', 1, $this->product_type['type']);
$this
->createDummyProductDisplayContentType();
$node = $this
->createDummyProductNode([
$this->product->product_id,
], 'Test Product Page');
$this->product_node_nid = $node->nid;
cache_clear_all();
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();
$this->baseCodeID = db_select('facebook_tracking_pixel_base_codes', 'c')
->fields('c', [
'base_code_id',
])
->condition('base_code_fbid', $this->baseCodeFBID, '=')
->execute()
->fetchField();
}
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');
$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');
}
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');
$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
->drupalGet('checkout');
$this
->assertRaw('facebook_tracking_pixel/commercetracking/fb_trk_checkoutstart_' . $this->baseCodeFBID . '.js', t('Checkout start tracking code JS found in head.'), 'FBTrkPx');
}
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');
$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
->drupalGet('checkout');
$edit = [];
$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');
}
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');
$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
->drupalGet('checkout');
$edit = [];
$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');
}
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');
$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');
$this
->drupalGet('');
$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 = [];
$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');
$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');
$edit = [];
$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');
}
}