diff --git a/tests/unit/Helpers/ArrayHelperTest.php b/tests/unit/Helpers/ArrayHelperTest.php index 346984092..4267b0f53 100644 --- a/tests/unit/Helpers/ArrayHelperTest.php +++ b/tests/unit/Helpers/ArrayHelperTest.php @@ -251,6 +251,92 @@ public function providerCanDetermineKeyExists(): Generator ]; } + /** + * Tests that the helper will return default with key not found. + * + * @covers ::get() + */ + public function testCanReturnDefaultWhenGetArrayValueByKeyNotFound() : void + { + $this->assertNull(ArrayHelper::get([], 'key'), 'ArrayHelper::get() does not return null by default as expected'); + $this->assertEquals('myDefault', ArrayHelper::get([], 'key', 'myDefault')); + $this->assertNotEquals('myDefault', ArrayHelper::get(['key' => 'value'], 'key', 'myDefault')); + } + + /** + * Tests that can retrieve array value by key without PHP error/warning. + * + * @covers ::get() + * @dataProvider providerCanGetArrayValueByKey + * + * @param mixed $array + * @param int|string $key + * @param mixed $expected + * @return void + */ + public function testCanGetArrayValueByKey($array, $key, $expected) : void + { + $this->assertSame($expected, ArrayHelper::get($array, $key)); + } + + /** @see testCanGetArrayValueByKey() */ + public function providerCanGetArrayValueByKey() : Generator + { + yield 'Existing string key in a key/value array' => [ + 'array' => ['key' => 'found'], + 'key' => 'key', + 'expected' => 'found', + ]; + + yield 'Nonexistent string key in a key/value array' => [ + 'array' => ['key' => 'notfound'], + 'key' => 'value', + 'expected' => null, + ]; + + yield 'Existing nested key with dot notation' => [ + 'array' => ['key' => ['nested' => ['deeply' => 'found']]], + 'key' => 'key.nested.deeply', + 'expected' => 'found', + ]; + + yield 'Nonexistent nested key with dot notation' => [ + 'array' => ['key' => ['nested' => ['deeply' => 'notfound']]], + 'key' => 'key.nested.more.deeply', + 'expected' => null, + ]; + + yield 'Existing dot-notated key as key is returned without iteration' => [ + 'array' => ['dot.notated.key' => 'found'], + 'key' => 'dot.notated.key', + 'expected' => 'found', + ]; + + yield 'Existing numeric index' => [ + 'array' => ['foo', 'bar', 'baz'], + 'key' => 1, + 'expected' => 'bar', + ]; + + yield 'Nonexistent numeric index' => [ + 'array' => ['foo', 'bar', 'baz'], + 'key' => 3, + 'expected' => null, + ]; + + yield 'Existing numeric string index' => [ + 'array' => ['foo', 'bar', 'baz'], + 'key' => '2', + 'expected' => 'baz', + ]; + + yield 'Nonexistent numeric string index' => [ + 'array' => ['foo', 'bar', 'baz'], + 'key' => '3', + 'expected' => null, + ]; + } + protected function getArrayAccessObject(): ArrayAccess { return new class implements ArrayAccess diff --git a/tests/unit/Helpers/PageHelperTest.php b/tests/unit/Helpers/PageHelperTest.php new file mode 100644 index 000000000..34f48d1d3 --- /dev/null +++ b/tests/unit/Helpers/PageHelperTest.php @@ -0,0 +1,81 @@ +expects('get_current_page') + ->once() + ->andReturn($pageData); + + $this->mockStaticMethod(PageHelper::class, 'getWooCommercePageController') + ->once() + ->andReturn($pageController); + + $this->assertSame($expected, PageHelper::isWooCommerceAnalyticsPage()); + } + + /** @see testCanDetermineIsWooCommerceAnalyticsPage */ + public function providerCanDetermineIsWooCommerceAnalyticsPage() : Generator + { + yield 'no page data' => [ + 'pageData' => false, + 'expected' => false, + ]; + + yield 'woocommerce home' => [ + 'pageData' => [ + 'id' => 'woocommerce-home', + 'parent' => 'woocommerce', + ], + 'expected' => false, + ]; + + yield 'orders page' => [ + 'pageData' => [ + 'id' => 'woocommerce-custom-orders', + ], + 'expected' => false, + ]; + + yield 'analytics overview' => [ + 'pageData' => [ + 'id' => 'woocommerce-analytics', + 'parent' => null, + ], + 'expected' => true, + ]; + + yield 'analytics revenue' => [ + 'pageData' => [ + 'id' => 'woocommerce-analytics-revenue', + 'parent' => 'woocommerce-analytics', + ], + 'expected' => true, + ]; + + yield 'analytics products' => [ + 'pageData' => [ + 'id' => 'woocommerce-analytics-products', + 'parent' => 'woocommerce-analytics', + ], + 'expected' => true, + ]; + } +} diff --git a/woocommerce/Helpers/ArrayHelper.php b/woocommerce/Helpers/ArrayHelper.php index c0092e383..dce943502 100644 --- a/woocommerce/Helpers/ArrayHelper.php +++ b/woocommerce/Helpers/ArrayHelper.php @@ -123,4 +123,33 @@ public static function exists($array, $key) : bool return array_key_exists($key, self::wrap($array)); } + + /** + * Gets an array value from a dot notated key. + * + * @param mixed $array + * @param int|string $key + * @param mixed $default + * @return mixed + */ + public static function get($array, $key, $default = null) + { + if (! self::accessible($array)) { + return $default; + } + + if (self::exists($array, $key)) { + return $array[$key]; + } + + foreach (explode('.', (string) $key) as $segment) { + if (! self::exists($array, $segment)) { + return $default; + } + + $array = $array[$segment]; + } + + return $array; + } } diff --git a/woocommerce/Helpers/PageHelper.php b/woocommerce/Helpers/PageHelper.php new file mode 100644 index 000000000..01db43d61 --- /dev/null +++ b/woocommerce/Helpers/PageHelper.php @@ -0,0 +1,57 @@ +get_current_page(); + + return ArrayHelper::get($pageData, 'id') === 'woocommerce-analytics' || + ArrayHelper::get($pageData, 'parent') === 'woocommerce-analytics'; + } + + /** + * @codeCoverageIgnore + */ + protected static function getWooCommercePageController() : ?\Automattic\WooCommerce\Admin\PageController + { + if (! class_exists(\Automattic\WooCommerce\Admin\PageController::class)) { + return null; + } + + return \Automattic\WooCommerce\Admin\PageController::get_instance(); + } +} diff --git a/woocommerce/changelog.txt b/woocommerce/changelog.txt index ae5b1727d..87fe45c8a 100644 --- a/woocommerce/changelog.txt +++ b/woocommerce/changelog.txt @@ -1,6 +1,8 @@ *** SkyVerge WooCommerce Plugin Framework Changelog *** -2024.nn.nn - version 5.15.4 +2025.nn.nn - version 5.15.4 +* New: Added PageHelper class to assist in determining page contexts. +* New: Add a helper method to get WooCommerce object meta values. 2025.01.24 - version 5.15.3 * Fix - Add Merchant ID to Google Pay, distinguishing it from Gateway merchant ID