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

Tests: Reorganise tests into contextual files #272

Merged
merged 15 commits into from
Jun 17, 2021
Merged
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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"wp-coding-standards/wpcs": "^2.3.0",
"yoast/wp-test-utils": "dev-develop#c0d9edd2"
},
"autoload-dev": {
"psr-4": {
"Parsely\\Tests\\": "tests/"
}
},
"scripts": {
"coverage": [
"@php ./vendor/bin/phpunit --coverage-html ./build/coverage-html"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</php>
<testsuites>
<testsuite name="WP_Tests">
<directory>./tests/</directory>
<directory suffix="-test.php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
27 changes: 19 additions & 8 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -1935,10 +1935,14 @@ public function get_first_image( $post ) {
}

/**
* Add parsely tracking to facebook instant articles
* Add Parse.ly tracking to Facebook Instant Articles.
*
* @param type $registry The registry info for fbia.
* @return string
* No returned value as Facebook passes the $registry by reference.
*
* @see https://github.com/Automattic/facebook-instant-articles-wp/blob/b8a0d4e44dc26578234e32d281b5560256abf5f3/class-instant-articles-post.php#L840
* @see https://github.com/Automattic/facebook-instant-articles-wp/blob/b8a0d4e44dc26578234e32d281b5560256abf5f3/wizard/class-instant-articles-option.php#L143-L146
*
* @param array $registry The registry info for FBIA.
*/
public function insert_parsely_tracking_fbia( &$registry ) {
$options = $this->get_options();
Expand All @@ -1963,15 +1967,22 @@ public function insert_parsely_tracking_fbia( &$registry ) {
'name' => $display_name,
'payload' => $embed_code,
);
}

return $embed_code;
/**
* Verify if request is an AMP request.
*
* @return bool True is an AMP request, false otherwise.
*/
public function is_amp_request() {
return function_exists( 'amp_is_request' ) && amp_is_request();
}

/**
* Add amp actions.
* Add AMP actions.
*/
public function parsely_add_amp_actions() {
if ( ! function_exists( 'is_amp_endpoint' ) || ! is_amp_endpoint() ) {
if ( ! $this->is_amp_request() ) {
return '';
}

Expand All @@ -1988,8 +1999,8 @@ public function parsely_add_amp_actions() {
/**
* Add amp analytics.
*
* @param type $analytics The analytics object you want to add.
* @return type
* @param array $analytics The analytics object you want to add.
* @return array
*/
public function parsely_add_amp_analytics( $analytics ) {
$options = $this->get_options();
Expand Down
126 changes: 126 additions & 0 deletions tests/GetCurrentUrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* \Parsely::get_current_url() tests.
*
* @package Parsely\Tests
*/

namespace Parsely\Tests;

use Parsely;

/**
* \Parsely::get_current_url() tests.
*/
final class GetCurrentUrlTest extends TestCase {
/**
* Data provider for test_get_current_url
*
* @return array[]
*/
public function data_for_test_get_current_url() {
return array(
// Start cases with 'force_https_canonicals' = true.
array(
true,
'http://example.com',
'https://example.com',
),
array(
true,
'https://example.com',
'https://example.com',
),
array(
true,
'http://example.com:1234',
'https://example.com:1234',
),
array(
true,
'https://example.com:1234',
'https://example.com:1234',
),
array(
true,
'http://example.com:1234/foo/bar',
'https://example.com:1234/foo/bar',
),
array(
true,
'https://example.com:1234/foo/bar',
'https://example.com:1234/foo/bar',
),
// Start cases with 'force_https_canonicals' = false.
array(
false,
'http://example.com',
'http://example.com',
),
array(
false,
'https://example.com',
'http://example.com',
),
array(
false,
'http://example.com:1234',
'http://example.com:1234',
),
array(
false,
'https://example.com:1234',
'http://example.com:1234',
),
array(
false,
'http://example.com:1234/foo/bar',
'http://example.com:1234/foo/bar',
),
array(
false,
'https://example.com:1234/foo/bar',
'http://example.com:1234/foo/bar',
),
);
}

/**
* Test the get_current_url() method.
*
* @dataProvider data_for_test_get_current_url
* @covers \Parsely::get_current_url
* @uses \Parsely::__construct()
* @uses \Parsely::get_options()
* @uses \Parsely::update_metadata_endpoint()
*
* @param bool $force_https Force HTTPS Canonical setting value.
* @param string $home Home URL.
* @param string $expected Expected current URL.
*/
public function test_get_current_url( $force_https, $home, $expected ) {
$parsely = new Parsely();
$options = get_option( Parsely::OPTIONS_KEY );
$options['force_https_canonicals'] = $force_https;
update_option( Parsely::OPTIONS_KEY, $options );

update_option( 'home', $home );

// Test homepage.
$this->go_to( '/' );
$res = $parsely->get_current_url();
self::assertStringStartsWith( $expected, $res );

// Test a specific post.
$post_array = $this->create_test_post_array();
$post_id = $this->factory->post->create( $post_array );
$this->go_to( '/?p=' . $post_id );
$res = $parsely->get_current_url( 'post', $post_id );
self::assertStringStartsWith( $expected, $res );

// Test a random URL.
$this->go_to( '/random-url' );
$res = $parsely->get_current_url();
self::assertStringStartsWith( $expected, $res );
}
}
102 changes: 102 additions & 0 deletions tests/Integrations/AmpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* AMP integration tests.
*
* @package Parsely\Tests
*/

namespace Parsely\Tests\Integrations;

use Parsely;
use Parsely\Tests\TestCase;

/**
* Test AMP integration.
*/
final class AmpTest extends TestCase {
/**
* Check the AMP integration when plugin is not active or request is not an AMP request.
*
* @covers \Parsely::parsely_add_amp_actions
* @uses \Parsely::__construct
* @uses \Parsely::get_options()
* @uses \Parsely::is_amp_request()
* @group amp
*/
public function test_amp_integration_with_amp_plugin_not_active_or_not_an_AMP_request() {
// Mock the Parsely class, but only the is_amp_request() method. This leaves
// the AMP-related methods unmocked, and therefore testable.

$parsely_mock = $this->getMockBuilder( 'Parsely' )->setMethods( array( 'is_amp_request' ) )->getMock();

// On the first run, let is_amp_request() return false.
$parsely_mock->method( 'is_amp_request' )->willReturn( false );

self::assertSame( '', $parsely_mock->parsely_add_amp_actions() );
}

/**
* Check the AMP integration when plugin is active and a request is an AMP request.
*
* @covers \Parsely::parsely_add_amp_actions
* @uses \Parsely::__construct
* @uses \Parsely::get_options()
* @uses \Parsely::is_amp_request()
* @group amp
* @group settings
*/
public function test_amp_integration_with_amp_plugin_active_and_a_request_is_an_AMP_request() {
// Mock the Parsely class, but only the is_amp_request() method. This leaves
// the AMP-related methods unmocked, and therefore testable.

$parsely_mock = $this->getMockBuilder( 'Parsely' )->setMethods( array( 'is_amp_request' ) )->getMock();

$parsely_mock->method( 'is_amp_request' )->willReturn( true );

// Check with AMP marked as disabled.
$options = get_option( $parsely_mock::OPTIONS_KEY );
$options['disable_amp'] = true;
update_option( $parsely_mock::OPTIONS_KEY, $options );

// Check the early return because AMP is marked as disabled.
self::assertSame( '', $parsely_mock->parsely_add_amp_actions() );

// Now check with AMP not marked as disabled.
$options['disable_amp'] = false;
update_option( $parsely_mock::OPTIONS_KEY, $options );

// Null return, so check filters have been added.
self::assertNull( $parsely_mock->parsely_add_amp_actions() );
self::assertNotFalse( has_filter( 'amp_post_template_analytics', array( $parsely_mock, 'parsely_add_amp_analytics' ) ) );
self::assertNotFalse( has_filter( 'amp_analytics_entries', array( $parsely_mock, 'parsely_add_amp_native_analytics' ) ) );
}

/**
* Check the registration of Parse.ly with AMP.
*
* @covers \Parsely::parsely_add_amp_actions
* @covers \Parsely::parsely_add_amp_analytics
* @uses \Parsely::__construct
* @uses \Parsely::get_options()
* @uses \Parsely::is_amp_request()
* @group amp
* @group settings
*/
public function test_amp_integration_registration() {
$parsely = new Parsely();
$options = get_option( 'parsely' );
$analytics = array();

// If apikey is empty, $analytics are returned.
self::assertSame( $analytics, $parsely->parsely_add_amp_analytics( $analytics ) );

// Now set the key and test for changes.
$options['apikey'] = 'my-api-key.com';
update_option( 'parsely', $options );

$output = $parsely->parsely_add_amp_analytics( $analytics );

self::assertSame( 'parsely', $output['parsely']['type'] );
self::assertSame( 'my-api-key.com', $output['parsely']['config_data']['vars']['apikey'] );
}
}
44 changes: 44 additions & 0 deletions tests/Integrations/FbiaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Facebook Instant Articles integration tests.
*
* @package Parsely\Tests
*/

namespace Parsely\Tests\Integrations;

use Parsely;
use Parsely\Tests\TestCase;

/**
* Test Facebook Instant Articles integration.
*/
final class FbiaTest extends TestCase {
/**
* Check the Facebook Instant Articles integration.
*
* @covers \Parsely::insert_parsely_tracking_fbia
* @uses \Parsely::__construct
* @uses \Parsely::get_options()
* @group fbia
*/
public function test_fbia_integration() {
$parsely = new Parsely();
$options = get_option( $parsely::OPTIONS_KEY );
$options['apikey'] = 'my-api-key.com';
update_option( 'parsely', $options );
$registry = array();

$parsely->insert_parsely_tracking_fbia( $registry );

// Check Parse.ly got added to the registry.
self::assertArrayHasKey( 'parsely-analytics-for-wordpress', $registry );

// Check display name assigned to the integration.
self::assertSame( 'Parsely Analytics', $registry['parsely-analytics-for-wordpress']['name'] );

// Check embed code contains a script (don't test for specifics), and the API key.
self::assertStringContainsString( '<script>', $registry['parsely-analytics-for-wordpress']['payload'] );
self::assertStringContainsString( $options['apikey'], $registry['parsely-analytics-for-wordpress']['payload'] );
}
}
Loading