Skip to content

Commit

Permalink
Integration tests for MS Sites List API Key Column
Browse files Browse the repository at this point in the history
  • Loading branch information
jblz committed Jan 13, 2022
1 parent 71ad94e commit 92928b7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/UI/class-network-admin-sites-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

declare(strict_types=1);

namespace Parsely;
namespace Parsely\UI;

use Parsely\Parsely;

Expand Down
81 changes: 74 additions & 7 deletions tests/Integration/UI/NetworkAdminSitesListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,113 @@
/**
* UI Tests for the Network Admin Sites List.
*
* @package Parsely\Tests\UI
* @package Parsely
*/

declare(strict_types=1);

namespace Parsely\Tests\Integration\UI;

// Explicit require as the `Network_Admin_Sites_List` class could not be found in test environment.
require_once __DIR__ . '/../../../src/UI/class-network-admin-sites-list.php';

use Parsely\Parsely;
use Parsely\Tests\Integration\TestCase;
use Parsely\UI\Network_Admin_Sites_List;
use \WP_Site;

/**
* UI Tests for the Network Admin Sites List.
*/
final class NetworkAdminSitesListTest extends TestCase {
/**
* Hold an insance of Network_Admin_Sites_List
*
* @var Network_Admin_Sites_List
*/
private static $sites_list;

/**
* Hold an instance of WP_MS_Sites_List_Table
*
* @var WP_MS_Sites_List_Table
*/
public $table = false;

/**
* Skip all tests for non-multisite runs.
* Set up an instance variable to hold a `WP_MS_Sites_List_Table` object.
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
public function set_up(): void {
parent::set_up();

if ( ! is_multisite() ) {
self::markTestSkipped();
}

$this->table = _get_list_table( 'WP_MS_Sites_List_Table', array( 'screen' => 'ms-sites' ) );
$this->table = _get_list_table( 'WP_MS_Sites_List_Table', array( 'screen' => 'ms-sites' ) );
self::$sites_list = new Network_Admin_Sites_List( new Parsely() );
}

/**
* Make sure the custom column is included.
*
* @covers \Parsely\UI\Network_Admin_Sites_List::run
* @covers \Parsely\UI\Network_Admin_Sites_List::add_api_key_column
* @covers \Parsely\UI\WP_MS_Sites_List_Table::get_columns
* @return void
*/
public function test_api_key_column_is_present(): void {
self::$sites_list->run();
$columns = $this->table->get_columns();

// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
var_export( $columns );
self::assertArrayHasKey( 'parsely-api-key', $columns );
self::assertSame( 'Parse.ly API Key', $columns['parsely-api-key'] );
}

/**
* Make sure the custom column is populated with default data for no option and the API key when set.
*
* @covers \Parsely\UI\Network_Admin_Sites_List::run
* @covers \Parsely\UI\Network_Admin_Sites_List::populate_api_key_column
* @covers \Parsely\UI\WP_MS_Sites_List_Table::prepare_items
* @xcovers \Parsely\UI\WP_MS_Sites_List_Table::get_columns
* @return void
*/
public function test_api_key_column_is_correctly_printed(): void {
$blog_id_with_api_key = $this->factory->blog->create();
$blog_id_without_api_key = $this->factory->blog->create();

self::$sites_list->run();

update_blog_option( $blog_id_with_api_key, Parsely::OPTIONS_KEY, array( 'apikey' => 'parselyrocks.example.com' ) );

self::assertArrayNotHasKey( 'parsely-api-key', $columns );
$this->table->prepare_items();

self::assertCount( 3, $this->table->items, 'There should be the main site, the subsite with the apikey set, and a subsite without.' );

foreach ( $this->table->items as $site ) {
self::assertInstanceOf( WP_Site::class, $site );

ob_start();
$this->table->column_default( $site->to_array(), 'parsely-api-key' );
$api_key_col_value = ob_get_clean();

if ( $blog_id_with_api_key === (int) $site->blog_id ) {
self::assertSame(
'parselyrocks.example.com',
$api_key_col_value,
'The API key was not printed and should have been.'
);
} else {
self::assertSame(
'<em>Parse.ly API key is missing</em>',
$api_key_col_value,
'The default value was not printed and should have been.'
);
}
}
}
}
1 change: 1 addition & 0 deletions wp-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Parsely\Integrations\Integrations;
use Parsely\UI\Admin_Warning;
use Parsely\UI\Plugins_Actions;
use Parsely\UI\Network_Admin_Sites_List;
use Parsely\UI\Recommended_Widget;
use Parsely\UI\Row_Actions;
use Parsely\UI\Settings_Page;
Expand Down

0 comments on commit 92928b7

Please sign in to comment.