Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at phpunit tests #6

Merged
merged 20 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test

on:
push:
branches:
- develop
- master
pull_request:
branches:
- develop

jobs:
phpunit:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: getong/mariadb-action@v1.1

- name: Set PHP version
uses: shivammathur/setup-php@v1
with:
php-version: '7.2'
coverage: none

- name: Install dependencies
run: composer install

- name: Setup WP Tests
run: bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1

- name: PHPUnit
run: './vendor/bin/phpunit'
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ All notable changes to this project will be documented in this file, per [the Ke
- Initial private release of Gutenbridge.

[Unreleased]: https://github.com/10up/gutenbridge/compare/trunk...develop
[1.0.0]: https://github.com/10up/gutenbridge/tree/commit-hash-here
[1.0.0]: https://github.com/10up/gutenbridge/tree/1.0.0
4 changes: 2 additions & 2 deletions CREDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ The following acknowledges the Maintainers for this repository, those who have C

The following individuals are responsible for curating the list of issues, responding to pull requests, and ensuring regular releases happen.

[Darshan Sawardekar (@dsawardekar)](https://github.com/dsawardekar), [Taylor Lovett (@tlovett1)](https://github.com/tlovett1), [Jeffrey Paul](https://github.com/jeffpaul)
[Darshan Sawardekar (@dsawardekar)](https://github.com/dsawardekar), [Taylor Lovett (@tlovett1)](https://github.com/tlovett1), [Jeffrey Paul (@jeffpaul)](https://github.com/jeffpaul).

## Contributors

Thank you to all the people who have already contributed to this repository via bug reports, code, design, ideas, project management, translation, testing, etc.

[Darshan Sawardekar (@dsawardekar)](https://github.com/dsawardekar), [Taylor Lovett (@tlovett1)](https://github.com/tlovett1), [Jeffrey Paul](https://github.com/jeffpaul).
[Darshan Sawardekar (@dsawardekar)](https://github.com/dsawardekar), [Taylor Lovett (@tlovett1)](https://github.com/tlovett1), [Tyler Bailey (@TylerB24890)](https://github.com/TylerB24890), [Jeffrey Paul (@jeffpaul)](https://github.com/jeffpaul).

## Libraries

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gutenbridge is a WordPress plugin that transforms classic editor content to [Gut
## Installation

1. Clone the repository into your `/plugins` directory.
2. Inside the repository directory, run `npm install` and then `npm build`.
2. Inside the repository directory, run `npm install` and then `npm run build`.
3. Inside the repository directory, run `composer install`.

## Frequently Asked Questions
Expand Down
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// phpcs:enable
}

gutenbridge_define( 'GUTENBRIDGE_PLUGIN', __DIR__ . '/plugin.php' );
gutenbridge_define( 'GUTENBRIDGE_PLUGIN', __DIR__ . '/gutenbridge.php' );
gutenbridge_define( 'GUTENBRIDGE_VERSION', $plugin_version );
gutenbridge_define( 'GUTENBRIDGE_DIR', plugin_dir_path( __FILE__ ) );
gutenbridge_define( 'GUTENBRIDGE_URL', plugin_dir_url( __FILE__ ) );
Expand Down
10 changes: 10 additions & 0 deletions portkey.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"includes/Gutenbridge/*.php": {
"type": "plugin",
"test": "tests/Gutenbridge/%sTest.php"
},
"tests/Gutenbridge/*Test.php": {
"type": "test",
"alternate": "includes/Gutenbridge/%s.php"
}
}
32 changes: 32 additions & 0 deletions tests/Gutenbridge/AdminBarMenuSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Gutenbridge;

class AdminBarMenuSupportTest extends \WP_UnitTestCase {

public $support;

function setUp() {
parent::setUp();

$this->support = new AdminBarMenuSupport();
$this->support->container = $this;
}

function test_it_has_a_container() {
$this->assertNotEmpty( $this->support->container );
$this->assertSame( $this, $this->support->container );
}

function test_it_will_not_register_if_not_logged_in() {
$actual = $this->support->can_register();
$this->assertFalse( $actual );
}

function test_it_will_register_if_logged_in() {
wp_set_current_user( 1 );
$actual = $this->support->can_register();
$this->assertTrue( $actual );
}

}
61 changes: 61 additions & 0 deletions tests/Gutenbridge/AdminMenuSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Gutenbridge;

class AdminMenuSupportTest extends \WP_UnitTestCase {

public $support;

function setUp() {
parent::setUp();

$this->support = new AdminMenuSupport();
$this->support->container = $this;
}

function test_it_has_a_container() {
$this->assertSame( $this, $this->support->container );
}

function test_it_will_be_registered_on_admin_pages() {
$GLOBALS['current_screen'] =
$mock = $this->getMockBuilder( stdObject::class )
->setMethods( ['in_admin'] )
->getMock();

$mock->expects( $this->once() )
->method( 'in_admin' )
->will( $this->returnValue( true ) );

$actual = $this->support->can_register();
$this->assertTrue( $actual );

$GLOBALS['current_screen'] = null;
}

function test_it_will_be_registered_if_logged_in() {
wp_set_current_user( 1 );
$actual = $this->support->can_register();
$this->assertTrue( $actual );
}

function test_it_will_not_be_registered_by_default() {
$actual = $this->support->can_register();
$this->assertFalse( $actual );
}

function test_it_can_sort_classic_menu_item() {
$menu_items = [
[ 'Add New' ],
[ 'Categories' ],
[ 'Tags' ],
[ 'Add New (Classic)' ],
];

$this->support->sort_post_type_menu( $menu_items );

$this->assertEquals( 'Add New', $menu_items[0][0] );
$this->assertEquals( 'Add New (Classic)', $menu_items[1][0] );
}

}
76 changes: 76 additions & 0 deletions tests/Gutenbridge/AssetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Gutenbridge;

class AssetsTest extends \WP_UnitTestCase {

public $assets;

function setUp() {
parent::setUp();

$this->assets = new Assets();
}

function test_it_knows_if_not_on_block_editor() {
$actual = $this->assets->is_block_editor();
$this->assertFalse( $actual );
}

function test_it_knows_if_on_block_editor_screen() {
$GLOBALS['current_screen'] =
$mock = $this->getMockBuilder( stdObject::class )
->setMethods( ['is_block_editor'] )
->getMock();

$mock->expects( $this->once() )
->method( 'is_block_editor' )
->will( $this->returnValue( true ) );

$actual = $this->assets->is_block_editor();
$this->assertTrue( $actual );

$GLOBALS['current_screen'] = null;
}

function test_it_will_not_be_registered_on_frontend() {
$actual = $this->assets->can_register();
$this->assertFalse( $actual );
}

function test_it_will_be_registered_on_admin_screens() {
$GLOBALS['current_screen'] =
$mock = $this->getMockBuilder( stdObject::class )
->setMethods( ['in_admin'] )
->getMock();

$mock->expects( $this->once() )
->method( 'in_admin' )
->will( $this->returnValue( true ) );

$actual = $this->assets->can_register();
$this->assertTrue( $actual );

$GLOBALS['current_screen'] = null;
}

function test_it_registers_gutenbridge_script_on_registration() {
$this->assets->register();
$actual = wp_script_is( 'gutenbridge_editor', 'registered' );
$this->assertTrue( $actual );
}

function test_it_enqueues_script_on_assets_hook() {
$this->assets->register();
$actual = wp_script_is( 'gutenbridge_editor', 'queue' );

$this->assertFalse( $actual );

$this->assets->do_assets();

$actual = wp_script_is( 'gutenbridge_editor', 'queue' );

$this->assertTrue( $actual );
}

}
69 changes: 69 additions & 0 deletions tests/Gutenbridge/ClassicEditorSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Gutenbridge;

class ClassicEditorSupportTest extends \WP_UnitTestCase {

public $support;
public $post_supports;
public $classic_param;

function setUp() {
parent::setUp();

$this->support = new ClassicEditorSupport();
$this->support->container = $this;
}

function test_it_has_a_container() {
$this->assertSame( $this, $this->support->container );
}

function test_it_will_not_be_registered_by_default() {
$actual = $this->support->can_register();
$this->assertFalse( $actual );
}

function test_it_can_be_registered_on_admin_pages() {
$GLOBALS['current_screen'] =
$mock = $this->getMockBuilder( stdObject::class )
->setMethods( ['in_admin'] )
->getMock();

$mock->expects( $this->once() )
->method( 'in_admin' )
->will( $this->returnValue( true ) );

$actual = $this->support->can_register();
$this->assertTrue( $actual );

$GLOBALS['current_screen'] = null;
}

function test_it_will_disable_block_editor_if_post_does_not_support_gutenbridge() {
$this->post_supports = false;
$actual = $this->support->enable_block_editor( true, 1 );

$this->assertFalse( $actual );
}

function test_it_will_disable_block_editor_if_classic_query_param_is_present() {
$this->post_supports = true;
$this->classic_param = true;

$actual = $this->support->enable_block_editor( true, 1 );

$this->assertFalse( $actual );
}

/* helpers */

function post_supports_gutenbridge() {
return $this->post_supports;
}

function has_classic_param() {
return $this->classic_param;
}

}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/plugin.php';
require dirname( dirname( __FILE__ ) ) . '/gutenbridge.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

Expand Down