Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions tests/unit/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/Helpers/PageHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace SkyVerge\WooCommerce\PluginFramework\v5_15_4\Tests\Unit\Helpers;

use Generator;
use Mockery;
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers\PageHelper;
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\Tests\TestCase;

/**
* @coversDefaultClass \SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers\PageHelper
*/
final class PageHelperTest extends TestCase
{
/**
* @covers ::isWooCommerceAnalyticsPage()
* @dataProvider providerCanDetermineIsWooCommerceAnalyticsPage
* @throws \ReflectionException
*/
public function testCanDetermineIsWooCommerceAnalyticsPage($pageData, bool $expected) : void
{
$pageController = Mockery::mock(\Automattic\WooCommerce\Admin\PageController::class);
$pageController->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,
];
}
}
29 changes: 29 additions & 0 deletions woocommerce/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
57 changes: 57 additions & 0 deletions woocommerce/Helpers/PageHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* WooCommerce Plugin Framework
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade the plugin to newer
* versions in the future. If you wish to customize the plugin for your
* needs please refer to http://www.skyverge.com
*
* @package SkyVerge/WooCommerce/Plugin/Classes
* @author SkyVerge
* @copyright Copyright (c) 2013-2025, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/

namespace SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers;

class PageHelper
{
/**
* Determines whether the current page is the WooCommerce "Analytics" page.
*
* @since 5.15.4
*/
public static function isWooCommerceAnalyticsPage() : bool
{
if (! $controller = static::getWooCommercePageController()) {
return false;
}

$pageData = $controller->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();
}
}
4 changes: 3 additions & 1 deletion woocommerce/changelog.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading