Skip to content

Commit 38ee5eb

Browse files
committed
Added unit test to confirm user agent string contains Plugin name and…
04ecab2 … version number when making API requests
1 parent 43b9eed commit 38ee5eb

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

.github/workflows/test.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ jobs:
4949
# Folder names within the 'tests' folder to run tests in parallel.
5050
test-groups: [
5151
'acceptance/forms',
52-
'acceptance/general'
52+
'acceptance/general',
53+
'wpunit'
5354
]
5455

5556
# Steps to install, configure and run tests

tests/wpunit.suite.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ modules:
1818
domain: "%TEST_SITE_WP_DOMAIN%"
1919
adminEmail: "%TEST_SITE_ADMIN_EMAIL%"
2020
title: "Test"
21-
plugins: ['convertkit-gravity-forms/convertkit.php'] # Change to the repository Plugin that we're testing.
22-
activatePlugins: ['convertkit-gravity-forms/convertkit.php'] # Change to the repository Plugin that we're testing.
21+
plugins: ['convertkit-gravity-forms/convertkit.php'] # Change to the repository Plugin that we're testing.

tests/wpunit/APITest.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Tests for the Integrate_ConvertKit_WPForms_API class.
4+
*
5+
* @since 1.3.1
6+
*/
7+
class APITest extends \Codeception\TestCase\WPTestCase
8+
{
9+
/**
10+
* The testing implementation.
11+
*
12+
* @var \WpunitTester.
13+
*/
14+
protected $tester;
15+
16+
/**
17+
* Holds the ConvertKit API class.
18+
*
19+
* @since 1.3.1
20+
*
21+
* @var CKGF_API
22+
*/
23+
private $api;
24+
25+
/**
26+
* Performs actions before each test.
27+
*
28+
* @since 1.3.1
29+
*/
30+
public function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
// Activate Plugin, to include the Plugin's constants in tests.
35+
activate_plugins('convertkit-gravity-forms/convertkit.php');
36+
37+
// Include class from /includes to test, as they won't be loaded by the Plugin
38+
// because Gravity Forms is not active.
39+
require_once 'includes/class-wp-ckgf.php';
40+
41+
// Initialize the classes we want to test.
42+
$this->api = new CKGF_API( $_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET'] );
43+
}
44+
45+
/**
46+
* Performs actions after each test.
47+
*
48+
* @since 1.3.1
49+
*/
50+
public function tearDown(): void
51+
{
52+
// Destroy the classes we tested.
53+
unset($this->api);
54+
55+
parent::tearDown();
56+
}
57+
58+
/**
59+
* Test that the User Agent string is in the expected format and
60+
* includes the Plugin's name and version number.
61+
*
62+
* @since 1.3.1
63+
*/
64+
public function testUserAgent()
65+
{
66+
// When an API call is made, inspect the user-agent argument.
67+
add_filter(
68+
'http_request_args',
69+
function($args, $url) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
70+
$this->assertStringContainsString(
71+
CKGF_PLUGIN_NAME . '/' . CKGF_PLUGIN_VERSION,
72+
$args['user-agent']
73+
);
74+
return $args;
75+
},
76+
10,
77+
2
78+
);
79+
80+
// Perform a request.
81+
$result = $this->api->account();
82+
}
83+
}

0 commit comments

Comments
 (0)