Skip to content

Commit 1307cea

Browse files
committed
Added tests
1 parent 7f52e34 commit 1307cea

9 files changed

Lines changed: 1954 additions & 0 deletions

tests/Integration/APITest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,56 @@ public function testAccessTokenRefreshedAndSavedWhenExpired()
110110
$this->assertEquals($_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], $account['refresh_token']);
111111
}
112112

113+
/**
114+
* Test that the Access Token, Refresh Token and Token Expiry are deleted from the Plugin's settings
115+
* when the Access Token used is invalid.
116+
*
117+
* @since 1.8.9
118+
*/
119+
public function testAccessTokenDeletedWhenInvalid()
120+
{
121+
// Save an invalid access token and refresh token in the Plugin's settings.
122+
wpforms_update_providers_options(
123+
'convertkit',
124+
array(
125+
'access_token' => 'invalidAccessToken',
126+
'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'],
127+
'token_expires' => time() + 10000,
128+
'label' => 'ConvertKit WordPress',
129+
'date' => time(),
130+
),
131+
'wpunittest1234'
132+
);
133+
134+
// Confirm the tokens saved.
135+
$providers = wpforms_get_providers_options();
136+
$account = reset( $providers['convertkit'] );
137+
$this->assertEquals( $account['access_token'], 'invalidAccessToken' );
138+
$this->assertEquals( $account['refresh_token'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] );
139+
140+
// Initialize the API using the invalid access token.
141+
$api = new \Integrate_ConvertKit_WPForms_API(
142+
$_ENV['CONVERTKIT_OAUTH_CLIENT_ID'],
143+
$_ENV['KIT_OAUTH_REDIRECT_URI'],
144+
$account['access_token'],
145+
$account['refresh_token']
146+
);
147+
148+
// Run request.
149+
$result = $api->get_account();
150+
151+
// Confirm a WP_Error is returned.
152+
$this->assertInstanceOf( 'WP_Error', $result );
153+
$this->assertEquals( $result->get_error_code(), 'convertkit_api_error' );
154+
$this->assertEquals( $result->get_error_message(), 'The access token is invalid' );
155+
156+
// Confirm tokens removed from the Plugin's settings, which confirms the `convertkit_api_access_token_invalid` hook was called when the tokens were deleted.
157+
$providers = wpforms_get_providers_options();
158+
$account = reset( $providers['convertkit'] );
159+
$this->assertEmpty( $account['access_token'] );
160+
$this->assertEmpty( $account['refresh_token'] );
161+
}
162+
113163
/**
114164
* Test that a WordPress Cron event is created when an access token is obtained.
115165
*
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use lucatume\WPBrowser\TestCase\WPTestCase;
6+
7+
/**
8+
* Tests for the Integrate_ConvertKit_WPForms_Resource_Custom_Fields class when no data is present in the API.
9+
*
10+
* @since 1.8.9
11+
*/
12+
class ResourceCustomFieldsNoDataTest extends \Codeception\TestCase\WPTestCase
13+
{
14+
/**
15+
* The testing implementation.
16+
*
17+
* @var \WpunitTester
18+
*/
19+
protected $tester;
20+
21+
/**
22+
* Holds the API instance.
23+
*
24+
* @since 1.8.9
25+
*
26+
* @var Integrate_ConvertKit_WPForms_API
27+
*/
28+
private $api;
29+
30+
/**
31+
* Holds the ConvertKit Resource class.
32+
*
33+
* @since 1.8.9
34+
*
35+
* @var Integrate_ConvertKit_WPForms_Resource_Custom_Fields
36+
*/
37+
private $resource;
38+
39+
/**
40+
* Holds the WPForms Account ID to store the Access Token and Refresh Token in WPForms's settings.
41+
*
42+
* @since 1.8.9
43+
*
44+
* @var string
45+
*/
46+
protected $wpforms_account_id = 'kit-unit-test';
47+
48+
/**
49+
* Performs actions before each test.
50+
*
51+
* @since 1.8.9
52+
*/
53+
public function setUp(): void
54+
{
55+
parent::setUp();
56+
57+
// Activate Plugin, to include the Plugin's constants in tests.
58+
activate_plugins('wpforms-lite/wpforms.php');
59+
activate_plugins('convertkit-wpforms/integrate-convertkit-wpforms.php');
60+
61+
// Include classes from /includes to test, as they won't be loaded by the Plugin
62+
// because WPForms is not active.
63+
require_once 'includes/class-integrate-convertkit-wpforms-api.php';
64+
require_once 'includes/class-integrate-convertkit-wpforms-resource-custom-fields.php';
65+
66+
// Storing Access Token and Refresh Token in WPForms's settings.
67+
wpforms_update_providers_options(
68+
'convertkit',
69+
array(
70+
'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'],
71+
'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'],
72+
'token_expires' => time() + 10000,
73+
'label' => 'ConvertKit WordPress',
74+
'date' => time(),
75+
),
76+
$this->wpforms_account_id
77+
);
78+
79+
// Initialize the API instance.
80+
$this->api = new \Integrate_ConvertKit_WPForms_API(
81+
$_ENV['CONVERTKIT_OAUTH_CLIENT_ID'],
82+
$_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'],
83+
$_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'],
84+
$_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA']
85+
);
86+
87+
// Initialize the resource class we want to test with the API instance and WPForms Account ID.
88+
$this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id );
89+
$this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources);
90+
91+
// Initialize the resource class, fetching resources from the API and caching them in the options table.
92+
$result = $this->resource->init();
93+
94+
// Confirm calling init() didn't result in an error.
95+
$this->assertNotInstanceOf(\WP_Error::class, $result);
96+
}
97+
98+
/**
99+
* Performs actions after each test.
100+
*
101+
* @since 1.8.9
102+
*/
103+
public function tearDown(): void
104+
{
105+
// Disable integration, removing Access Token and Refresh Token from Plugin's settings.
106+
wpforms_update_providers_options(
107+
'convertkit',
108+
array(
109+
'access_token' => '',
110+
'refresh_token' => '',
111+
'token_expires' => 0,
112+
'label' => 'ConvertKit WordPress',
113+
'date' => time(),
114+
),
115+
$this->wpforms_account_id
116+
);
117+
118+
// Delete Resources from options table.
119+
delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id);
120+
delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried');
121+
122+
// Destroy the classes we tested.
123+
unset($this->api);
124+
unset($this->resource);
125+
126+
parent::tearDown();
127+
}
128+
129+
/**
130+
* Test that the refresh() function performs as expected.
131+
*
132+
* @since 1.8.9
133+
*/
134+
public function testRefresh()
135+
{
136+
// Confirm that no resources exist in the stored options table data.
137+
$result = $this->resource->refresh();
138+
$this->assertNotInstanceOf(\WP_Error::class, $result);
139+
$this->assertIsArray($result);
140+
$this->assertCount(0, $result);
141+
}
142+
143+
/**
144+
* Test that the expiry timestamp is set and returns the expected value.
145+
*
146+
* @since 1.8.9
147+
*/
148+
public function testExpiry()
149+
{
150+
// Define the expected expiry date based on the resource class' $cache_duration setting.
151+
$expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration);
152+
153+
// Fetch the actual expiry date set when the resource class was initialized.
154+
$expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration);
155+
156+
// Confirm both dates match.
157+
$this->assertEquals($expectedExpiryDate, $expiryDate);
158+
}
159+
160+
/**
161+
* Test that the get() function performs as expected.
162+
*
163+
* @since 1.8.9
164+
*/
165+
public function testGet()
166+
{
167+
// Confirm that no resources exist in the stored options table data.
168+
$result = $this->resource->get();
169+
$this->assertNotInstanceOf(\WP_Error::class, $result);
170+
$this->assertIsArray($result);
171+
$this->assertCount(0, $result);
172+
}
173+
174+
/**
175+
* Test that the count() function returns the number of resources.
176+
*
177+
* @since 1.8.9
178+
*/
179+
public function testCount()
180+
{
181+
$result = $this->resource->get();
182+
$this->assertEquals($this->resource->count(), count($result));
183+
}
184+
185+
/**
186+
* Test that the exist() function performs as expected.
187+
*
188+
* @since 1.8.9
189+
*/
190+
public function testExist()
191+
{
192+
// Confirm that the function returns false, because resources do not exist.
193+
$result = $this->resource->exist();
194+
$this->assertSame($result, false);
195+
}
196+
}

0 commit comments

Comments
 (0)