Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken codeception tests
Browse files Browse the repository at this point in the history
The previous commit introduced some changes that caused codeception
unit tests to break. This commit fixes this by introducing a new
service class Transifex_Live_Integration_WP_Services to wrap WP API
functionality. This refactoring enables mocking the WP API in the
tests.
Also a new test class, BaseTestCase, is introduced to cater for
different PHPUnit versions between production and development
environment.
deathbird committed Apr 2, 2019
1 parent ae7814f commit baafb71
Showing 9 changed files with 88 additions and 16 deletions.
6 changes: 4 additions & 2 deletions includes/lib/transifex-live-integration-rewrite.php
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ public function __construct( $settings, $rewrite_options ) {
Plugin_Debug::logTrace();
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/common/transifex-live-integration-validators.php';
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/override/transifex-live-integration-generate-rewrite-rules.php';
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/lib/transifex-live-integration-wp-services.php';
$this->rewrite_options = [ ];
$this->languages_regex = $settings['languages_regex'];
$this->source_language = $settings['source_language'];
@@ -97,6 +98,7 @@ public function __construct( $settings, $rewrite_options ) {
$this->rewrite_pattern = $pattern;
}
}
$this->wp_services = new Transifex_Live_Integration_WP_Services();
}

public function get_language_url( $atts ) {
@@ -146,7 +148,7 @@ function wp_hook() {
* @return string Returns modified link
*/

static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
$pattern ) {
Plugin_Debug::logTrace();
if ( !(isset( $pattern )) ) {
@@ -170,7 +172,7 @@ static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
if ( count( $m ) > 1 ) {
$link = str_replace( $m[1], $lang, $m[0] );
} else {
$site_host = parse_url(site_url())['host'];
$site_host = parse_url($this->wp_services->get_site_url())['host'];
$parsed_url = parse_url($link);
$link_host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
// change only wordpress links - not links reffering to other domains
18 changes: 18 additions & 0 deletions includes/lib/transifex-live-integration-wp-services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* @package TransifexLiveIntegration
*/

/**
* Provides WP services to other classes.
*/
class Transifex_Live_Integration_WP_Services {

/*
* Wraps WP site_url().
*/
function get_site_url() {
return site_url();
}
}
15 changes: 15 additions & 0 deletions tests/unit/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/*
* This class caters for the different PHPUnit versions between production (CI)
* and development.
* To execute unit tests use :
*
* php codecept.phar run unit --debug
*/
if (class_exists('\PHPUnit_Framework_TestCase')) {
// php 5.6, production - CI environment
class BaseTestCase extends \PHPUnit_Framework_TestCase {}
} else {
// php 7+, dev environment
class BaseTestCase extends PHPunit\Framework\TestCase {}
}
3 changes: 2 additions & 1 deletion tests/unit/CalculateDefaultsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class CalculateDefaultsTest extends \PHPUnit_Framework_TestCase
class CalculateDefaultsTest extends BaseTestCase
{
private $data_subdomain;
private $data_subdirectory;
3 changes: 2 additions & 1 deletion tests/unit/GenerateLanguageUrlMapTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class GenerateLanguageUrlMapTest extends \PHPUnit_Framework_TestCase
class GenerateLanguageUrlMapTest extends BaseTestCase
{

private $data;
3 changes: 2 additions & 1 deletion tests/unit/GenerateTokenizedUrlTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class GenerateTokenizedUrlTest extends \PHPUnit_Framework_TestCase
class GenerateTokenizedUrlTest extends BaseTestCase
{

private $data;
3 changes: 2 additions & 1 deletion tests/unit/PrerenderCheckTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class PrerenderCheckTest extends \PHPUnit_Framework_TestCase
class PrerenderCheckTest extends BaseTestCase
{

private $data;
4 changes: 3 additions & 1 deletion tests/unit/ReplaceSubdomainTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class ReplaceSubdomainTest extends \PHPUnit_Framework_TestCase
class ReplaceSubdomainTest extends BaseTestCase
{

private $data;
@@ -9,6 +10,7 @@ protected function setUp()
{
include_once './includes/common/plugin-debug.php';
include_once './includes/common/transifex-live-integration-common.php';
include_once './includes/transifex-live-integration-util.php';
$this->data = [
[
'page_url' => 'https://www.foo.bar/about/',
49 changes: 40 additions & 9 deletions tests/unit/ReverseHardLinkTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<?php
/**
* Execute this test setting <environment> either as 'dev' or 'prod' :
*
* export TEST_ENV=<environment>
* php codecept.phar run unit --debug ReverseHardLinkTest
*/
include_once __DIR__ .'/BaseTestCase.php';

class ReverseHardLinkTest extends \PHPUnit_Framework_TestCase {
class ReverseHardLinkTest extends BaseTestCase {

private $data;

protected function setUp() {
include_once './includes/common/plugin-debug.php';
include_once './includes/lib/transifex-live-integration-rewrite.php';
include_once './includes/lib/transifex-live-integration-wp-services.php';
include_once '../../../wp-load.php';
$this->data = [[ //1
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/www.mydomain.com\/(zh_CN|de_DE)\//',
'result' => 'http://www.mydomain.com/zh_CN/page-markup-and-formatting'
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/www.mydomain.com\/(zh_CN|de_DE)\//',
'result' => 'http://www.mydomain.com/zh_CN/page-markup-and-formatting'
],
[ //2
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
@@ -24,6 +35,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //3
'host' => 'http://www.mydomain.com',
'lang' => 'zh_HK',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
@@ -32,6 +44,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //4
'host' => 'http://www.mydomain.com',
'lang' => 'cn',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ],
@@ -40,6 +53,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/cn/page-markup-and-formatting'
],
[ //5
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => [ ],
@@ -48,6 +62,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //6
'host' => 'http://www.mydomain.com',
'lang' => null,
'link' => 'http://www.mydomain.com/',
'languages_map' => null,
@@ -56,6 +71,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/'
],
[ //7
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ],
@@ -64,6 +80,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //8
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
@@ -72,6 +89,7 @@ protected function setUp() {
'result' => 'http://zh_CN.mydomain.com/page-markup-and-formatting'
],
[ //9
'host' => 'http://www.mydomain.com',
'lang' => 'cn',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ],
@@ -80,6 +98,7 @@ protected function setUp() {
'result' => 'http://cn.mydomain.com/page-markup-and-formatting'
],
[ //10 plex case
'host' => 'http://www.mydomain.com',
'lang' => 'de',
'link' => 'http://www.mydomain.com/page-markup-and-formatting-de',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ],
@@ -88,25 +107,37 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/de/page-markup-and-formatting-de'
],
[ //11
'host' => 'http://www.mydomain.com',
'lang' => 'de',
'link' => 'http://www.mydomain.com/page-markup-and-formatting-de',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/(cn|de|www).mydomain.com\/.*/',
'result' => 'http://de.mydomain.com/page-markup-and-formatting-de'
],
[ //12 external link, leave intact
'host' => 'http://www.mydomain.com',
'lang' => 'de',
'link' => 'http://www.another.com/page-markup-and-formatting-de',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/www.mydomain.com\/(cn|de)\//',
'result' => 'http://www.another.com/page-markup-and-formatting-de'
]
];
}

public function testMe() {
$counter = 0;
$rewrite = new Transifex_Live_Integration_Rewrite( [], [] );
foreach ($this->data as $i) {
$counter = $counter + 1;
$result = Transifex_Live_Integration_Rewrite::reverse_hard_link(
$rewrite->wp_services = \Codeception\Stub::make(Transifex_Live_Integration_WP_Services::class, ['get_site_url' => $i['host']]);
$result = $rewrite->reverse_hard_link(
$i['lang'], $i['link'], $i['languages_map'], $i['souce_lang'], $i['pattern']
);

// eval(\Psy\sh());
// codecept_debug('result:' . $i['result']);
$this->assertEquals( $i['result'], $result, 'Test Number:' . $counter );
}
}

0 comments on commit baafb71

Please sign in to comment.