Skip to content
Open
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ indent_style = tab
space_around_brackets = true
indent_size = 4

[*.php]
indent_style = space

[{.jshintrc,*.json,*.yml}]
indent_style = space
indent_size = 2
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Code Style

on:
pull_request:
paths:
- '**.php'

jobs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
fetch-depth: 0

- name: Run php-cs-fixer
uses: docker://oskarstark/php-cs-fixer-ga

- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: '(auto): apply php-cs-fixer changes'
push_options: '--force'
env:
GITHUB_TOKEN: ${{ secrets.YARD_BOT_PAT }}
11 changes: 8 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: xdebug
tools: composer:v2
coverage: none
coverage: xdebug

- name: Install dependencies
run: composer update --no-interaction --no-progress --prefer-dist --no-scripts

- name: Execute tests
run: vendor/bin/phpunit --testsuite "Unit Test Suite"

run: vendor/bin/phpunit --testsuite "Unit Test Suite" --coverage-clover coverage.xml

- name: Coverage Report as Comment (Clover)
if: github.event_name == 'pull_request'
uses: lucassabreu/comment-coverage-clover@main
with:
file: coverage.xml

# Job to check if certain files have changed
what-has-changed:
Expand Down
2 changes: 1 addition & 1 deletion config/seopress_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
'_seopress_social_twitter_img_width',
'_seopress_social_twitter_img_height',
]
];
];
42 changes: 15 additions & 27 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
<phpunit
bootstrap="tests/Unit/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
testdox="true"
>
<testsuites>
<testsuite name="Unit Test Suite">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/Base/</directory>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="true"/>
</php>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/Unit/bootstrap.php" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" testdox="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/Base/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit Test Suite">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="true"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/Base/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function filterPostLink(string $link, WP_Post $post, bool $leavename, $sa
*/
public function filterPreviewLink(string $link, WP_Post $post): string
{
if ($post->post_type !== 'openpub-item' || ! $this->plugin->settings->isPortalSlugValid()) {
if ($post->post_type !== 'openpub-item' || ! $this->plugin->settings->isPortalSlugValid()) {
return $link;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Base/Foundation/DependencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function checkVersion(array $dependency): bool
{
try {
$file = file_get_contents(WP_PLUGIN_DIR . '/' . $dependency['file']);
} catch(\Exception $e) {
} catch (\Exception $e) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Base/Metabox/Commands/AbstractConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function execute(): void
foreach ($this->flattenArray($holder) as $item) {
try {
$this->convert($item);
} catch(Exception $e) {
} catch (Exception $e) {
WP_CLI::error(sprintf('Something went wrong with converting item [%s]. Error: %s', $item->post_title, $e->getMessage()));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Base/Metabox/Commands/ConvertExpirationDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function convertDateToTimeStamp(WP_Post $item): ?int

try {
$object = new DateTime($date);
} catch(Exception $e) {
} catch (Exception $e) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Base/RestAPI/Controllers/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace OWC\OpenPub\Base\RestAPI\Controllers;

use WP_Post;
use OWC\OpenPub\Base\Repositories\Theme;
use WP_Error;
use WP_Post;
use WP_REST_Request;
use OWC\OpenPub\Base\Repositories\Theme;

class ThemeController extends BaseController
{
Expand Down
2 changes: 1 addition & 1 deletion src/Base/RestAPI/ItemFields/ExpiredField.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private function getExpiredStatus(WP_Post $post): array
$timezone = \wp_timezone_string();

$dateNow = new DateTime('now', new DateTimeZone($timezone));
$date = new DateTime(date('Y-m-d H:i:s', $date), new DateTimeZone($timezone)); // The date is saved in the timezone of the Wordpress installation.
$date = (new DateTime())->setTimestamp($date)->setTimezone(new DateTimeZone($timezone)); // The date is saved in the timezone of the Wordpress installation.

return [
'message' => ($date->getTimestamp() < $dateNow->getTimestamp()) ? 'Item is expired' : '',
Expand Down
4 changes: 2 additions & 2 deletions src/Base/Settings/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function register()

public function registerMissingNumberField($field, $escapedValue, $objectID, $objectType, $fieldTypeObject): void
{
echo $fieldTypeObject->input(array( 'type' => 'number' ));
echo $fieldTypeObject->input([ 'type' => 'number' ]);
}

public function registerSettingsPages(): void
Expand All @@ -43,7 +43,7 @@ public function registerSettingsPages(): void
}
}

protected function registerSettingsPage(array $page): void
public function registerSettingsPage(array $page): void
{
$fields = $page['fields'] ?? [];
unset($page['fields']); // Fields will be added later on.
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Base/ElasticPress/ElasticPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,29 @@ public function it_sets_the_correct_index_name()
$indexName = '';
$siteID = 1;

putenv('environment=development');
$_ENV['environment'] = 'development';

$expected = 'owc-openpub--1--development';
$actual = $this->service->setIndexNameByEnvironment($indexName, $siteID);

$this->assertEquals($expected, $actual);

putenv('environment=test');
$_ENV['environment'] = 'test';

$expected = 'owc-openpub--1--test';
$actual = $this->service->setIndexNameByEnvironment($indexName, $siteID);

$this->assertEquals($expected, $actual);

putenv('environment=');
$_ENV['environment'] = '';

$expected = 'owc-openpub--1';
$actual = $this->service->setIndexNameByEnvironment($indexName, $siteID);

$this->assertEquals($expected, $actual);

define('EP_INDEX_PREFIX', 'prefix');
putenv('environment=test');
$_ENV['environment'] = 'test';

$expected = 'prefix--owc-openpub--1--test';
$actual = $this->service->setIndexNameByEnvironment($indexName, $siteID);
Expand Down
130 changes: 23 additions & 107 deletions tests/Unit/Base/Metabox/MetaboxServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OWC\OpenPub\Base\Foundation\Config;
use OWC\OpenPub\Base\Foundation\Loader;
use OWC\OpenPub\Base\Foundation\Plugin;
use OWC\OpenPub\Base\Metabox\AdminNotice;
use OWC\OpenPub\Base\Metabox\MetaboxServiceProvider;
use OWC\OpenPub\Tests\TestCase;
use WP_Mock;
Expand Down Expand Up @@ -55,12 +56,20 @@ public function check_registration_of_metaboxes()

$service = new MetaboxServiceProvider($plugin);

$plugin->loader->shouldReceive('addFilter')->withArgs([
'rwmb_meta_boxes',
$plugin->loader->shouldReceive('addAction')->withArgs([
'cmb2_admin_init',
$service,
'registerMetaboxes',
10,
1
0
])->once();

$plugin->loader->shouldReceive('addAction')->withArgs([
'admin_notices',
\Mockery::type(AdminNotice::class),
'upgradeAdminNotice',
10,
0
])->once();

$service->register();
Expand Down Expand Up @@ -100,113 +109,20 @@ public function check_registration_of_metaboxes()
]
];

$prefix = MetaboxServiceProvider::PREFIX;

$expectedMetaboxes = [
0 => [
'id' => 'metadata',
'fields' => [
[
'type' => 'heading'
],
[
'id' => $prefix . 'metabox_id1'
],
[
'id' => $prefix . 'metabox_id2'
]
]
],
1 => [
'id' => 'escape_element',
'fields' => [
[
'type' => 'heading'
],
[
'id' => $prefix . 'metabox_id1'
],
[
'id' => $prefix . 'metabox_id2'
]
]
]
];

$config->shouldReceive('get')->with('metaboxes')->once()->andReturn($configMetaboxes);
$config->shouldReceive('get')->with('escape_element_metabox')->once()->andReturn($configMetaboxes);

//test for filter being called
\WP_Mock::expectFilter('owc/openpub/base/before-register-metaboxes', $expectedMetaboxes);

$this->assertEquals($expectedMetaboxes, $service->registerMetaboxes([]));

$existingMetaboxes = [
0 => [
'id' => 'existing_metadata',
'fields' => [
[
'type' => 'existing_heading'
],
[
'id' => $prefix . 'existing_metabox_id1'
],
[
'id' => $prefix . 'existing_metabox_id2'
]
]
]
];
$config->shouldReceive('get')->with('cmb2_metaboxes')->once()->andReturn($configMetaboxes['base']);
$config->shouldReceive('get')->with('escape_element_metabox')->once()->andReturn($configMetaboxes['escape_element']);

$expectedMetaboxesAfterMerge = [
$cmb2 = \Mockery::mock('CMB2');
$cmb2->shouldReceive('add_field')->with(['type' => 'heading']);
$cmb2->shouldReceive('add_field')->with(['id' => '_owc_metabox_id1']);
$cmb2->shouldReceive('add_field')->with(['id' => '_owc_metabox_id2']);

0 => [
'id' => 'existing_metadata',
'fields' => [
[
'type' => 'existing_heading'
],
[
'id' => $prefix . 'existing_metabox_id1'
],
[
'id' => $prefix . 'existing_metabox_id2'
]
]
],
1 => [
'id' => 'metadata',
'fields' => [
[
'type' => 'heading'
],
[
'id' => $prefix . 'metabox_id1'
],
[
'id' => $prefix . 'metabox_id2'
]
]
],
2 => [
'id' => 'escape_element',
'fields' => [
[
'type' => 'heading'
],
[
'id' => $prefix . 'metabox_id1'
],
[
'id' => $prefix . 'metabox_id2'
]
]
]
];
\WP_Mock::userFunction('new_cmb2_box', [
'return' => $cmb2,
]);

$config->shouldReceive('get')->with('metaboxes')->once()->andReturn($configMetaboxes);
$config->shouldReceive('get')->with('escape_element_metabox')->once()->andReturn($configMetaboxes);
$service->registerMetaboxes();

$this->assertEquals($expectedMetaboxesAfterMerge, $service->registerMetaboxes($existingMetaboxes));
$this->expectNotToPerformAssertions();
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Base/PostType/PostTypeServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function check_registration_of_posttypes()
'orderByPublishedDate',
])->once();

$plugin->loader->shouldReceive('addAction')->withArgs([
'wp_insert_post_data',
$service,
'fillPostName',
10,
4,
])->once();

/**
* Examples of registering post types: http://johnbillion.com/extended-cpts/
*/
Expand Down
Loading