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

Adds unit tests and is_module_active method to SAL. #11010

Merged
merged 7 commits into from
Dec 21, 2018
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</testsuite>
<testsuite name="json-api">
<file>tests/php/test_class.json-api-jetpack-endpoints.php</file>
<directory prefix="test_" suffix=".php">tests/php/sal</directory>
</testsuite>
<testsuite name="infinite-scroll">
<directory prefix="test_" suffix=".php">tests/php/modules/infinite-scroll</directory>
Expand Down
2 changes: 2 additions & 0 deletions sal/class.json-api-site-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ abstract public function is_jetpack();

abstract public function get_jetpack_modules();

abstract public function is_module_active( $module );

abstract public function is_vip();

abstract public function is_multisite();
Expand Down
8 changes: 4 additions & 4 deletions sal/class.json-api-site-jetpack-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ function after_render_options( &$options ) {
}

function get_jetpack_modules() {
if ( is_user_member_of_blog() ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we remove the check from here we will want to add it in json-endpoints/class.wpcom-json-api-get-site-endpoint.php

return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
}
return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
}

return null;
function is_module_active( $module ) {
return in_array ( $module, Jetpack_Options::get_option( 'active_modules', array() ), true );
}

function is_vip() {
Expand Down
54 changes: 54 additions & 0 deletions tests/php/sal/test_class.json-api-platform-jetpack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require_once dirname( __FILE__ ) . '/../../../sal/class.json-api-platform.php';

class SalSiteTest extends WP_UnitTestCase {
static $token;
static $site;

static function setUpBeforeClass( ) {
parent::setUpBeforeClass();

self::$token = (object) array(
'blog_id' => get_current_blog_id(),
'user_id' => get_current_user_id(),
'external_user_id' => 2,
'role' => 'administrator'
);

$platform = wpcom_get_sal_platform( self::$token );

self::$site = $platform->get_site( self::$token->blog_id );
}

function test_uses_synced_api_post_type_whitelist_if_available() {

$this->assertFalse( self::$site->is_post_type_allowed( 'my_new_type' ) );
}

function test_is_module_active() {

// Picking random 3 modules from an array of existing ones to not slow down the test
$modules = array_rand( Jetpack::get_available_modules(), 3 );

foreach ( $modules as $module ) {
Jetpack::deactivate_module( $module );

$this->assertEquals(
Jetpack::is_module_active( $module ),
self::$site->is_module_active( $module )
);

Jetpack::activate_module( $module );

$this->assertEquals(
Jetpack::is_module_active( $module ),
self::$site->is_module_active( $module )
);
}
}

function test_interface() {
$this->assertTrue( method_exists( 'SAL_Site', 'is_module_active' ) );
}
}
39 changes: 39 additions & 0 deletions tests/php/sal/test_class.json-api-post-jetpack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

require_once dirname( __FILE__ ) . '/../../../sal/class.json-api-platform.php';

class SalPostsTest extends WP_UnitTestCase {
static $token;
static $site;

static function setUpBeforeClass() {
parent::setUpBeforeClass();

self::$token = (object) array(
'blog_id' => get_current_blog_id(),
'user_id' => get_current_user_id(),
'external_user_id' => 2,
'role' => 'administrator'
);

$platform = wpcom_get_sal_platform( self::$token );

self::$site = $platform->get_site( self::$token->blog_id );
}

function test_returns_content_wrapped_in_a_post_object() {
// Insert the post into the database
$post_id = wp_insert_post( array(
'post_title' => 'Title',
'post_content' => 'The content.',
'post_status' => 'publish',
'post_author' => get_current_user_id()
) );

$post = get_post( $post_id );

$wrapped_post = self::$site->wrap_post( $post, 'display' );

$this->assertEquals( $post->post_type, $wrapped_post->get_type() );
}
}