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

Fix broken codeception tests #201

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions includes/lib/transifex-live-integration-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 )) ) {
Expand All @@ -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
Expand Down
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 {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This assumes that PHP 7+ is used for developing, which may not be always the case (for example if an engineer uses another docker-compose file with an older PHP version). I wonder if we can try / catch the error if the first TestCase class doesn't exist, then load the alternative one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bravo @Diontsoumas, excellent idea! In fact PHP has the function class_exists() just for that ;)

}
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;
Expand Down
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;
Expand Down
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;
Expand Down
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;
Expand Down
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;
Expand All @@ -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/',
Expand Down
48 changes: 39 additions & 9 deletions tests/unit/ReverseHardLinkTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
<?php
/**
* Execute this test as follows :
*
* 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" ],
Expand All @@ -24,6 +34,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" ],
Expand All @@ -32,6 +43,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" ],
Expand All @@ -40,6 +52,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' => [ ],
Expand All @@ -48,6 +61,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,
Expand All @@ -56,6 +70,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" ],
Expand All @@ -64,6 +79,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" ],
Expand All @@ -72,6 +88,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" ],
Expand All @@ -80,6 +97,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" ],
Expand All @@ -88,25 +106,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 );
}
}
Expand Down