Skip to content

Commit

Permalink
Merge pull request #6 from 10up/feature/first-pass-at-phpunit-tests
Browse files Browse the repository at this point in the history
First pass at phpunit tests
  • Loading branch information
tlovett1 authored Oct 9, 2020
2 parents 1c5cf13 + 9a11ee9 commit 7183062
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 4 deletions.
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/[email protected]

- 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'
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 @@ Convert to Blocks is a WordPress plugin that transforms classic editor content t
## 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
10 changes: 10 additions & 0 deletions portkey.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"includes/ConvertToBlocks/*.php": {
"type": "plugin",
"test": "tests/ConvertToBlocks/%sTest.php"
},
"tests/ConvertToBlocks/*Test.php": {
"type": "test",
"alternate": "includes/ConvertToBlocks/%s.php"
}
}
32 changes: 32 additions & 0 deletions tests/ConvertToBlocks/AdminBarMenuSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ConvertToBlocks;

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/ConvertToBlocks/AdminMenuSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace ConvertToBlocks;

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/ConvertToBlocks/AssetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace ConvertToBlocks;

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( 'convert_to_blocks_editor', 'registered' );
$this->assertTrue( $actual );
}

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

$this->assertFalse( $actual );

$this->assets->do_assets();

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

$this->assertTrue( $actual );
}

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

namespace ConvertToBlocks;

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_convert_to_blocks() {
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__ ) ) . '/convert-to-blocks.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

Expand Down

0 comments on commit 7183062

Please sign in to comment.