Skip to content

Commit 041a922

Browse files
authored
Merge pull request #116 from Kit/ignore-invalid-resource-arrays
Ignore invalid Resource arrays
2 parents b761658 + 68bfc3c commit 041a922

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/class-convertkit-resource-v4.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ public function get_by_id( $id ) {
181181
*/
182182
public function get_by( $key, $value ) {
183183

184-
// Don't mutate the underlying resources, so multiple calls to get()
185-
// with different order_by and order properties are supported.
186-
$resources = $this->resources;
187-
188184
// Don't attempt sorting if no resources exist.
189185
if ( ! $this->exist() ) {
190-
return $resources;
186+
return false;
191187
}
192188

189+
// Don't mutate the underlying resources, so multiple calls to get()
190+
// with different order_by and order properties are supported.
191+
$resources = $this->resources;
192+
193193
foreach ( $resources as $id => $resource ) {
194194
// Remove this resource if it doesn't have the array key.
195195
if ( ! array_key_exists( $key, $resource ) ) {
@@ -332,6 +332,21 @@ public function exist() {
332332
return false;
333333
}
334334

335+
// If the resources are not an array, no resources exist.
336+
if ( ! is_array( $this->resources ) ) {
337+
return false;
338+
}
339+
340+
// Ignore cached resources stored in the options table if they are a flat array
341+
// that originates from between 1.6.0 and 1.9.5.2 of the Plugin
342+
// i.e. [id => name], rather than [id => [id => '...', name => '...', ...]].
343+
// The Plugin's upgrade routine must handle converting the flat array to the new array format, but the
344+
// resource classes are typically loaded before this happens, so this check is necessary to avoid
345+
// fatal errors when a user upgrades from 1.6.0...1.9.5.2 to 1.9.6 or higher.
346+
if ( ! is_array( reset( $this->resources ) ) ) {
347+
return false;
348+
}
349+
335350
return ( count( $this->resources ) ? true : false );
336351

337352
}

tests/Integration/ResourceTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,34 @@ public function testGetByMultipleValues()
258258
$this->assertEquals('Z Name', end($result)[ $this->resource->order_by ]);
259259
}
260260

261+
/**
262+
* Tests that the get_by() function returns false when queried with invalid resource data.
263+
*
264+
* @since 2.1.6
265+
*/
266+
public function testGetByWithInvalidResourceData()
267+
{
268+
// Mock invalid resource data.
269+
$this->mockInvalidData();
270+
271+
// Assert result is false.
272+
$this->assertFalse($this->resource->get_by('name', 'Z Name'));
273+
}
274+
275+
/**
276+
* Tests that the exist() function returns false when queried with invalid resource data.
277+
*
278+
* @since 2.1.6
279+
*/
280+
public function testExistWithInvalidResourceData()
281+
{
282+
// Mock invalid resource data.
283+
$this->mockInvalidData();
284+
285+
// Assert result is false.
286+
$this->assertFalse($this->resource->exist());
287+
}
288+
261289
/**
262290
* Tests that the refresh() function for Forms returns resources in an array, and that they are
263291
* in alphabetical ascending order by default.
@@ -669,4 +697,17 @@ private function mockData()
669697
],
670698
];
671699
}
700+
701+
/**
702+
* Defines an array of resources when mocking resource data in tests,
703+
* in a format stored in the WordPress Plugins prior to using these Libraries.
704+
*
705+
* @since 2.1.6
706+
*/
707+
private function mockInvalidData()
708+
{
709+
$this->resource->resources = [
710+
2780977 => 'Resource',
711+
];
712+
}
672713
}

0 commit comments

Comments
 (0)