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