-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Github Actions workflow Add UT
- Loading branch information
Showing
10 changed files
with
430 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
/woocommerce-kelkoogroup-salestracking.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.