Skip to content

Commit

Permalink
feature/implement s2s calls
Browse files Browse the repository at this point in the history
  Add Github Actions workflow
  Add UT
  • Loading branch information
Joel Desannaux committed Jun 5, 2024
2 parents 3222ba5 + 7b9c25a commit cd43dcd
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/1-Support.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assignees: ''
We don't offer technical support on GitHub so we recommend using the following:

**Reading our documentation**
Usage docs can be found here: https://www.kelkoogroup.com/kelkoo-customer-service/support-for-merchants/sales-tracking-guides/
Usage docs can be found here: https://developers.kelkoogroup.com/app/documentation/navigate/_merchant/salesTrackingWS/_/_/Overview


**General usage and development questions**
Expand Down
121 changes: 121 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
name: CI

on:
push:
branches:
- 'bugfix/*'
- 'feature/*'
pull_request:
branches:
- main
- master

jobs:
merge-branches:
runs-on: ubuntu-latest
steps:
- name: Set Git identity
run: |
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git config --global user.name "${GITHUB_ACTOR}"
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 0 # Ensure all branches and history are fetched

- name: Fetch all branches
run: git fetch --all

- name: Delete remote dev branch if exists
run: git push origin --delete dev || true

- name: Create dev branch from master
run: |
git checkout -b dev origin/master
git push origin dev --force
- name: Merge feature branch into dev
run: |
git checkout dev
git merge --no-ff ${{ github.event.ref }}
git push origin dev --force
build:
runs-on: ubuntu-latest
needs: merge-branches

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: CI
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping -h localhost" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
ini-values: error_reporting=-1
- name: Install and configure WordPress for Testing
run: |
ls -l
git clone --depth=1 --branch=trunk https://github.com/WordPress/wordpress-develop.git wordpress
cp wordpress/wp-tests-config-sample.php wordpress/wp-tests-config.php
sed -i "s/youremptytestdbnamehere/CI/" wordpress/wp-tests-config.php
sed -i "s/yourusernamehere/root/" wordpress/wp-tests-config.php
sed -i "s/yourpasswordhere/root/" wordpress/wp-tests-config.php
sed -i "s|localhost|127.0.0.1|" wordpress/wp-tests-config.php
- name: Install WordPress dependencies
run: |
cd wordpress/src
composer require wp-cli/wp-cli-bundle
./vendor/bin/wp --version
./vendor/bin/wp config create --dbname=CI --dbuser=root --dbpass=root --dbhost=127.0.0.1
./vendor/bin/wp core install --url=http://localhost --title="Test Site" --admin_user=admin --admin_password=password [email protected] --skip-email
- name: Copy plugin files on wordpress
run: |
rsync -av --exclude='wordpress' --exclude='.git' $GITHUB_WORKSPACE/ wordpress/src/wp-content/plugins/kelkoo-sales-tracking
- name: Install and Activate WooCommerce and KST plugins
run: |
cd wordpress/src
./vendor/bin/wp plugin install woocommerce --activate
echo "Wordpress version:"
./vendor/bin/wp core version
./vendor/bin/wp plugin activate kelkoo-sales-tracking
echo
echo "Plugins installed:"
./vendor/bin/wp plugin list
echo
echo "Tables presents :"
./vendor/bin/wp db tables
- name: Install PHPUnit and Polyfills
run: |
cd wordpress/src
if [ ! -f composer.json ]; then
echo '{}' > composer.json
fi
composer require --dev phpunit/phpunit yoast/phpunit-polyfills
ls -l vendor/yoast/phpunit-polyfills
find -iname phpunit
- name: Run plugin tests
env:
WP_RUN_CORE_TESTS: 0
WP_TESTS_PHPUNIT_POLYFILLS_PATH: ${{ github.workspace }}/wordpress/vendor/yoast/phpunit-polyfills
run: |
cd wordpress/src
echo "WP_RUN_CORE_TESTS=$WP_RUN_CORE_TESTS"
echo "WP_TESTS_PHPUNIT_POLYFILLS_PATH=$WP_TESTS_PHPUNIT_POLYFILLS_PATH"
./vendor/bin/phpunit --bootstrap wp-content/plugins/kelkoo-sales-tracking/tests/bootstrap.php --configuration wp-content/plugins/kelkoo-sales-tracking/phpunit.xml.dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
/woocommerce-kelkoogroup-salestracking.iml
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ To install the Kelkoo Group sales tracking plugin, you can have a look at [Kelko
5. Complete the form with information related to your kelkoo group subscription
6. Done!

*Installation via Wordpress Cli*
1. Log in to your site with SSH
2. cd to your sites root wordpress folder
3. Run wp plugin install ```wp plugin install kelkoogroup-sales-tracking --activate```
4. On plugin page go to “Settings” and click on the “Kelkoogroup” link
5. Complete the form with information related to your kelkoo group subscription
6. Done!

*Installation from source*
1. Go to folder `/wp-content/plugins` and unzip `woocommerce-kelkoogroup-sales-tracking` file
Expand All @@ -32,7 +39,6 @@ To install the Kelkoo Group sales tracking plugin, you can have a look at [Kelko
4. Complete the form with information related to your kelkoo group subscription
5. Done!


## Settings
- Configure the kelkoogroup setting via Settings > Kelkoogroup

Expand Down
6 changes: 5 additions & 1 deletion inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ function kelkoogroup_salestracking_call_leadtag_js() {
}

/**
* Store identifiers from URL parameters
* Stores identifiers from URL parameters in transients.
*
* This function iterates over predefined URL parameters and their associated transient keys.
* If any of these parameters are present in the URL, their values are stored in corresponding transients
* with a lifespan of 365 days.
*/
function kelkoogroup_salestracking_store_identifiers_from_url() {
// Define an array of parameters and their associated keys
Expand Down
11 changes: 11 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
stopOnFailure="false"
stopOnError="false">
<testsuites>
<testsuite name="Kelkoogroup Sales Tracking Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
16 changes: 11 additions & 5 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ To install the Kelkoo Group sales tracking plugin:
5. Complete the form with information related to your kelkoo group subscription
6. Done!

*Installation via Wordpress Cli*
1. Log in to your site with SSH
2. cd to your sites root wordpress folder
3. Run wp plugin install ```wp plugin install kelkoogroup-sales-tracking --activate```
4. On plugin page go to “Settings” and click on the “Kelkoogroup” link
5. Complete the form with information related to your kelkoo group subscription
6. Done!

*Installation via FTP Client Software*

Expand All @@ -47,16 +54,15 @@ To install the Kelkoo Group sales tracking plugin:
6. Done!


*Installation via wordpress cli*
*Installation via Wordpress Cli*

1. Launch activation command through the wordpress-cli
```
wp plugin install kelkoogroup-sales-tracking --activate --path=/var/www/html
```
2. Login to your WordPress admin dashboard, go to Plugins, Click `Activate`
3. On plugin page go to “Settings” and click on the “Kelkoogroup” link
4. Complete the form with information related to your kelkoo group subscription
5. Done!
2. On plugin page go to “Settings” and click on the “Kelkoogroup” link
3. Complete the form with information related to your kelkoo group subscription
4. Done!


*Kelkoo Group settings*
Expand Down
174 changes: 174 additions & 0 deletions tests/Kelkoogroup_SalesTracking_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
/**
* Test case for Kelkoogroup Sales Tracking plugin.
*/
class Kelkoogroup_SalesTracking_Test extends WP_UnitTestCase {

/**
* Test if Kelkoogroup_SalesTracking class exists.
*/
public function test_class_exists() {
$this->assertTrue( class_exists( 'Kelkoogroup_SalesTracking' ) );
}

/**
* Test if Kelkoogroup_SalesTracking class is instantiable.
*/
public function test_class_is_instantiable() {
$kelkoogroup_salestracking = new Kelkoogroup_SalesTracking();
$this->assertInstanceOf( 'Kelkoogroup_SalesTracking', $kelkoogroup_salestracking );
}

/**
* Tests if identifiers from URL parameters are stored in transients.
*/
public function test_kelkoogroup_salestracking_store_identifiers_from_url() {
$_GET = array(
'kk' => 'test_kk_identifier',
'gclid' => 'test_gclid_identifier',
'msclkid' => 'test_msclkid_identifier',
'other_param' => 'test_other_param'
);

kelkoogroup_salestracking_store_identifiers_from_url();

$this->assertEquals('test_kk_identifier', get_transient('kelkoogroup_salestracking_kk_identifier'));
$this->assertEquals('test_gclid_identifier', get_transient('kelkoogroup_salestracking_gclid_identifier'));
$this->assertEquals('test_msclkid_identifier', get_transient('kelkoogroup_salestracking_msclkid_identifier'));

// Vérifier si les valeurs des autres paramètres ne sont pas stockées dans les transients
$this->assertFalse(get_transient('kelkoogroup_salestracking_other_param_identifier'));
}


/**
* Test if Kelkoogroup_SalesTracking class methods are callable.
*/
public function test_class_methods() {
$kelkoogroup_salestracking = new Kelkoogroup_SalesTracking();

$this->assertTrue( method_exists( $kelkoogroup_salestracking, 'kelkoogroup_salestracking_setup' ) );
$this->assertTrue( method_exists( $kelkoogroup_salestracking, 'kelkoogroup_salestracking_woocommerce_thankyou' ) );
$this->assertTrue( method_exists( $kelkoogroup_salestracking, 'kelkoogroup_salestracking_send_server_side_request' ) );
$this->assertTrue( method_exists( $kelkoogroup_salestracking, 'kelkoogroup_salestracking_generate_sale_id' ) );
$this->assertTrue( method_exists( $kelkoogroup_salestracking, 'kelkoogroup_salestracking_encode_basket' ) );
$this->assertTrue( method_exists( $kelkoogroup_salestracking, 'kelkoogroup_salestracking_custom_base64_encode' ) );
}

/**
* Test if Kelkoogroup_SalesTracking settings initialization works.
*/
public function test_settings_init() {
$kelkoogroup_salestracking = new Kelkoogroup_SalesTracking();
$kelkoogroup_salestracking->kelkoogroup_salestracking_setup();

$this->assertNotFalse(has_action('admin_menu', 'kelkoogroup_salestracking_add_admin_menu'));
$this->assertNotFalse(has_action('admin_init', 'kelkoogroup_salestracking_settings_init'));
}

/**
* Test if kelkoogroup_salestracking_construct_kelkoogroup_request_url correctly constructs the URL with the given parameters.
*/
public function test_kelkoogroup_salestracking_construct_kelkoogroup_request_url() {
$order = $this->createMock(WC_Order::class);
$order->method('get_order_number')->willReturn('12345');
$order->method('get_total')->willReturn(100.00);

$productsKelkoo = array(
array(
'productname' => 'Product 1',
'productid' => 1,
'quantity' => 2,
'price' => 10.00
),
array(
'productname' => 'Product 2',
'productid' => 2,
'quantity' => 1,
'price' => 20.00
)
);

$campaign = array(
'country' => 'fr',
'merchantId' => '123'
);

// Mock get_transient function to return null for simplicity
$this->plugin_instance = $this->getMockBuilder(Kelkoogroup_SalesTracking::class)
->setMethods(['kelkoogroup_salestracking_encode_basket', 'kelkoogroup_salestracking_generate_sale_id'])
->getMock();

$this->plugin_instance->method('kelkoogroup_salestracking_encode_basket')->willReturn('VGVzdCBkYXRhIGZvciBlbmNvZGluZw');
$this->plugin_instance->method('kelkoogroup_salestracking_generate_sale_id')->willReturn('0.55');

$expected_url = 'https://s.kelkoogroup.net/st?country=fr&orderId=12345&comId=123&orderValue=100&productsInfos=VGVzdCBkYXRhIGZvciBlbmNvZGluZw&saleId=0.55&source=serverToServer';

$constructed_url = $this->plugin_instance->kelkoogroup_salestracking_construct_kelkoogroup_request_url($order, $productsKelkoo, $campaign);

// Assert that the constructed URL matches the expected URL
$this->assertEquals($expected_url, $constructed_url);
}

/**
* Test if kelkoogroup_salestracking_encode_basket correctly encodes the products array.
*/
public function test_kelkoogroup_salestracking_encode_basket() {
$productsArray = array(
array(
'productname' => 'Product 1',
'productid' => 1,
'quantity' => 2,
'price' => 10.00
),
array(
'productname' => 'Product 2',
'productid' => 2,
'quantity' => 1,
'price' => 20.00
)
);

$this->plugin_instance = new Kelkoogroup_SalesTracking();
$encoded_basket = $this->plugin_instance->kelkoogroup_salestracking_encode_basket($productsArray);

// Expected encoded basket value
$expected_json = '[{"productname":"Product 1","productid":1,"quantity":2,"price":10},{"productname":"Product 2","productid":2,"quantity":1,"price":20}]';
$expected_base64 = 'W3sicHJvZHVjdG5hbWUiOiJQcm9kdWN0IDEiLCJwcm9kdWN0aWQiOjEsInF1YW50aXR5IjoyLCJwcmljZSI6MTB9LHsicHJvZHVjdG5hbWUiOiJQcm9kdWN0IDIiLCJwcm9kdWN0aWQiOjIsInF1YW50aXR5IjoxLCJwcmljZSI6MjB9XQ';
$expected_encoded_basket = urlencode($expected_base64);

// Assert that the encoded basket matches the expected value
$this->assertEquals($expected_encoded_basket, $encoded_basket);
}

/**
* Test if kelkoogroup_salestracking_custom_base64_encode correctly encodes data.
*/
public function test_kelkoogroup_salestracking_custom_base64_encode() {
$this->plugin_instance = new Kelkoogroup_SalesTracking();
$data = 'Test data for encoding';

$encoded_data = $this->plugin_instance->kelkoogroup_salestracking_custom_base64_encode($data);

// Expected encoded data using custom base64 encoding
$expected_data = 'VGVzdCBkYXRhIGZvciBlbmNvZGluZw';
// Assert that the custom base64 encoded data matches the expected data
$this->assertEquals($expected_data, $encoded_data);
}

/**
* Test if kelkoogroup_salestracking_generate_sale_id returns a float between 0 and 1.
*/
public function test_kelkoogroup_salestracking_generate_sale_id() {
$this->plugin_instance = new Kelkoogroup_SalesTracking();

$sale_id = $this->plugin_instance->kelkoogroup_salestracking_generate_sale_id();

// Assert that the sale ID is a float
$this->assertIsFloat($sale_id);
// Assert that the sale ID is between 0 and 1
$this->assertGreaterThanOrEqual(0, $sale_id);
$this->assertLessThanOrEqual(1, $sale_id);
}

}
Loading

0 comments on commit cd43dcd

Please sign in to comment.