Skip to content

Commit

Permalink
test: add test for homepage and tax archive
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Mar 6, 2024
1 parent 98cf654 commit 56b7de2
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 22 deletions.
20 changes: 18 additions & 2 deletions wp/headless-wp/tests/php/includes/PLLUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PLLUnitTestCase extends WP_UnitTestCase {
* @return void
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
self::$polylang = new \stdClass();

self::$polylang->options = \PLL_Install::get_default_options();
Expand Down Expand Up @@ -68,10 +69,10 @@ public function setUp(): void {
* @return void
*/
public function tearDown(): void {
parent::tearDown();

unset( $GLOBALS['wp_settings_errors'] );
self::$polylang->model->clean_languages_cache(); // We must do it before database ROLLBACK otherwhise it is impossible to delete the transient

parent::tearDown();
}

/**
Expand Down Expand Up @@ -120,6 +121,21 @@ protected static function delete_all_languages() {
}
}

/**
* Switches to the given language
*
* @param string $lang The language to switch to
*
* @return void
*/
protected static function switch_language( string $lang ): void {
$language = \PLL()->model->get_language( $lang );

if ( false !== $language ) {
\PLL()->curlang = $language;
}
}

/**
* Backport assertNotFalse to PHPUnit 3.6.12 which only runs in PHP 5.2.
*
Expand Down
154 changes: 134 additions & 20 deletions wp/headless-wp/tests/php/tests/TestPolylangIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace HeadlessWP\Tests;

use HeadlessWP\Integrations\YoastSEO;
use HeadlessWP\Tests\Inc\PLLUnitTestCase;
use WP_REST_Request;
use WP_REST_Server;
use WP_Rewrite;

/**
Expand All @@ -22,41 +24,38 @@ class TestPolylangIntegration extends PLLUnitTestCase {
*/
protected \WP_Rewrite $wp_rewrite;


/**
* The rest server
*
* @var WP_REST_Server
*/
protected static \WP_REST_Server $rest_server;

/**
* Setup any pre-test data.
*
* @return void
*/
public static function setUpBeforeClass(): void {
$plugins = [ 'polylang/polylang.php', 'wordpress-seo/wp-seo.php' ];

foreach ( $plugins as $plugin ) {
if ( ! is_plugin_active( $plugin ) ) {
activate_plugin( $plugin );
}
}

// Make sure these are all active.
foreach ( $plugins as $plugin ) {
self::assertTrue( is_plugin_active( $plugin ) );
}

parent::setUpBeforeClass();

self::create_language( 'en_US' );
self::create_language( 'pt_BR' );

self::$polylang = new \PLL_Frontend( self::$polylang->links_model );
self::$polylang->init();

self::$rest_server = rest_get_server();
}

/**
* Sets up the tests
*
* @return void
*/
public function set_up() {
parent::set_up();
public function setUp(): void {
parent::setUp();

/**
* The rewrite class
Expand All @@ -78,18 +77,36 @@ public function set_up() {
* Returns the yoast head for a post
*
* @param integer $post_id The post id
* @param string $post_type The post type
*
* @return string
*/
protected function getYoastHeadForPost( int $post_id ): string {
$request = new WP_REST_Request( 'GET', "/wp/v2/posts/$post_id" );
$response = rest_do_request( $request );
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
protected function getYoastHeadForPost( int $post_id, string $post_type = 'post' ): string {
$post_type = get_post_type_object( $post_type );
$request = new WP_REST_Request( 'GET', "/wp/v2/$post_type->rest_base/$post_id" );
$response = rest_do_request( $request );
$data = self::$rest_server->response_to_data( $response, false );

return $data['yoast_head'] ?? '';
}

/**
* Returns the yoast head for a term
*
* @param string $post_type The post type we're fetching from
* @param int $term_id The term id
*
* @return string
*/
protected function getYoastHeadForTerm( string $post_type, int $term_id ): string {
$post_type = get_post_type_object( $post_type );
$request = new WP_REST_Request( 'GET', "/wp/v2/$post_type->rest_base", [ 'categories' => $term_id ] );
$response = rest_do_request( $request );
$data = self::$rest_server->response_to_data( $response, true );

return $data[0]['_embedded']['wp:term'][0][0]['yoast_head'] ?? '';
}

/**
* Test hreflang on single posts
*
Expand Down Expand Up @@ -135,4 +152,101 @@ public function test_hreflang_on_single_posts() {
$this->assertNotFalse( strpos( $yoast_head, '<link rel="alternate" href="http://localhost:8889/pt_br-post/" hreflang="pt" />' ), 'hreflang was not found' );
$this->assertNotFalse( strpos( $yoast_head, '<link rel="alternate" href="http://localhost:8889/en-post/" hreflang="en" />' ), 'hreflang was not found' );
}

/**
* Test hreflang on homepage
*
* @return void
*/
public function test_hreflang_on_homepage() {
$english_page = $this->factory()->post->create(
[
'post_title' => '[EN] home page',
'post_status' => 'publish',
'post_content' => 'english page',
'post_type' => 'page',
]
);

\pll_set_post_language( $english_page, 'en' );

$portuguese_page = $this->factory()->post->create(
[
'post_title' => '[PT_BR] home Page',
'post_status' => 'publish',
'post_content' => 'portugese page',
'post_type' => 'page',
]
);

\pll_set_post_language( $portuguese_page, 'pt' );

\pll_save_post_translations(
[
'en' => $english_page,
'pt' => $portuguese_page,
]
);

update_option( 'page_on_front', $english_page );

$yoast_head = $this->getYoastHeadForPost( $english_page, 'page' );

$this->assertNotFalse( strpos( $yoast_head, '<link rel="alternate" href="http://localhost:8889/en-home-page/" hreflang="en" />' ), 'hreflang was not found' );
$this->assertNotFalse( strpos( $yoast_head, '<link rel="alternate" href="http://localhost:8889/pt_br-home-page/" hreflang="pt" />' ), 'hreflang was not found' );
$this->assertNotFalse( strpos( $yoast_head, '<link rel="alternate" href="http://localhost:8889/" hreflang="x-default" />' ), 'hreflang was not found' );
}

/**
* Tests hrelang on taxonomy archives
*
* @return void
*/
public function test_hreflang_on_taxonomy_archive() {
$cat_en = $this->factory()->term->create_and_get(
[
'taxonomy' => 'category',
]
);

$cat_en_id = $cat_en->term_id;

\pll_set_term_language( $cat_en_id, 'en' );

$cat_pt = $this->factory()->term->create_and_get(
[
'taxonomy' => 'category',
]
);

$cat_pt_id = $cat_pt->term_id;

\pll_set_term_language( $cat_pt_id, 'pt' );

$posts_en = $this->factory()->post->create_many(
5,
[
'post_category' => [ $cat_en_id ],
]
);

foreach ( $posts_en as $post_en ) {
\pll_set_post_language( $post_en, 'en' );
}

$posts_pt = $this->factory()->post->create_many(
5,
[
'post_category' => [ $cat_pt_id ],
]
);

foreach ( $posts_pt as $post_pt ) {
\pll_set_post_language( $post_pt, 'pt' );
}

$yoast_head = $this->getYoastHeadForTerm( 'post', $cat_pt_id );

$this->assertNotFalse( strpos( $yoast_head, '<link rel="alternate" href="http://localhost:8889/category/english" hreflang="en" />' ), 'hreflang was not found' );
}
}

0 comments on commit 56b7de2

Please sign in to comment.