|
| 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