Skip to content

Commit

Permalink
PluginProperties // add support for non-default Plugin headers to acc…
Browse files Browse the repository at this point in the history
…ess via PluginProperties::get() and PluginProperties::has().

#13
  • Loading branch information
Chrico committed Jul 8, 2021
1 parent 4993d0f commit 6a0283d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/Properties/PluginProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ protected function __construct(string $pluginMainFile)
$pluginData = get_plugin_data($pluginMainFile);
$properties = Properties::DEFAULT_PROPERTIES;

// Map pluginData to internal structure.
foreach (self::HEADERS as $key => $pluginDataKey) {
$properties[$key] = $pluginData[$pluginDataKey] ?? '';
unset($pluginData[$pluginDataKey]);
}
$properties = array_merge($properties, $pluginData);

$this->pluginFile = $pluginMainFile;

Expand Down
86 changes: 78 additions & 8 deletions tests/unit/Properties/PluginPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function testIsActive(): void
static::assertTrue($testee->isActive());
}


/**
* @test
*/
Expand All @@ -109,11 +108,83 @@ public function testIsNetworkActive(): void
static::assertTrue($testee->isNetworkActive());
}

/**
* @test
* @dataProvider provideCustomHeaders
*
* @param array $customHeaders
*/
public function testCustomPluginHeaders(array $customHeaders): void
{
$expectedBaseName = 'plugin-name';
$expectedBasePath = '/path/to/plugin/';
$expectedAuthor = 'Inpsyde GmbH';
$expectedAuthorUri = 'https://www.inpsyde.com';

$pluginData = array_merge(
[
'Author' => $expectedAuthor,
'AuthorURI' => $expectedAuthorUri,

],
$customHeaders
);

Functions\expect('get_plugin_data')->andReturn($pluginData);
Functions\when('plugins_url')->returnArg(1);
Functions\expect('plugin_basename')->andReturn($expectedBaseName);
Functions\expect('plugin_dir_path')->andReturn($expectedBasePath);

$testee = PluginProperties::new($expectedBasePath);

// Check if PluginProperties do behave as normal
static::assertSame($expectedBaseName, $testee->baseName());
static::assertSame($expectedBasePath, $testee->basePath());

// Test default Headers
static::assertSame($expectedAuthor, $testee->author());
static::assertSame($expectedAuthor, $testee->get(Properties::PROP_AUTHOR));
static::assertSame($expectedAuthorUri, $testee->authorUri());
static::assertSame($expectedAuthorUri, $testee->get(Properties::PROP_AUTHOR_URI));

// Test headers from get_plugin_data() are removed from properties
// "Author" will be mapped to Properties::PROP_AUTHOR
static::assertFalse($testee->has('Author'));
static::assertFalse($testee->has('AuthorURI'));

// Test custom Headers
foreach ($customHeaders as $key => $value) {
static::assertTrue($testee->has($key));
static::assertSame($value, $testee->get($key));
}
}

/**
* Provides custom Plugin Headers which will
* be returned by get_plugin_data()
*/
public function provideCustomHeaders(): \Generator
{
yield 'WooCommerce Plugin Headers' => [
[
'WC requires at least' => '2.2',
'WC tested up to:' => '2.3',
],
];

yield 'Custom Plugin Headers' => [
[
'Foo' => 'bar',
'Baz' => 'bam',
],
];
}

/**
*
* @param string $pluginPath
* @param string $muPluginDir
* @param bool$expected
* @param bool $expected
*
* @test
*
Expand All @@ -125,7 +196,6 @@ public function testIsMuPlugin(string $pluginPath, string $muPluginDir, bool $ex
{
$expectedBaseName = 'plugin-name';


Functions\expect('get_plugin_data')->andReturn([]);
Functions\when('plugins_url')->returnArg(1);
Functions\expect('plugin_basename')->andReturn($expectedBaseName);
Expand All @@ -142,19 +212,19 @@ public function testIsMuPlugin(string $pluginPath, string $muPluginDir, bool $ex
/**
* @return array[]
*/
public function provideIsMuPluginData(): array {
public function provideIsMuPluginData(): array
{
return [
'is not mu-plugin' => [
'/wp-content/plugins/the-plugin/',
'/wp-content/mu-plugins/',
false
false,
],
'is mu-plugin' => [
'/wp-content/mu-plugins/the-plugin/',
'/wp-content/mu-plugins/',
true
]
true,
],
];
}

}

0 comments on commit 6a0283d

Please sign in to comment.