diff --git a/wp/headless-wp/tests/php/includes/PLLUnitTestCase.php b/wp/headless-wp/tests/php/includes/PLLUnitTestCase.php
index cbd1762d7..eefc6ac0c 100644
--- a/wp/headless-wp/tests/php/includes/PLLUnitTestCase.php
+++ b/wp/headless-wp/tests/php/includes/PLLUnitTestCase.php
@@ -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();
@@ -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();
}
/**
@@ -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.
*
diff --git a/wp/headless-wp/tests/php/tests/TestPolylangIntegration.php b/wp/headless-wp/tests/php/tests/TestPolylangIntegration.php
index 2901f5876..a01b985ae 100644
--- a/wp/headless-wp/tests/php/tests/TestPolylangIntegration.php
+++ b/wp/headless-wp/tests/php/tests/TestPolylangIntegration.php
@@ -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;
/**
@@ -22,25 +24,20 @@ 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' );
@@ -48,6 +45,8 @@ public static function setUpBeforeClass(): void {
self::$polylang = new \PLL_Frontend( self::$polylang->links_model );
self::$polylang->init();
+
+ self::$rest_server = rest_get_server();
}
/**
@@ -55,8 +54,8 @@ public static function setUpBeforeClass(): void {
*
* @return void
*/
- public function set_up() {
- parent::set_up();
+ public function setUp(): void {
+ parent::setUp();
/**
* The rewrite class
@@ -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
*
@@ -135,4 +152,101 @@ public function test_hreflang_on_single_posts() {
$this->assertNotFalse( strpos( $yoast_head, '' ), 'hreflang was not found' );
$this->assertNotFalse( strpos( $yoast_head, '' ), '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, '' ), 'hreflang was not found' );
+ $this->assertNotFalse( strpos( $yoast_head, '' ), 'hreflang was not found' );
+ $this->assertNotFalse( strpos( $yoast_head, '' ), '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, '' ), 'hreflang was not found' );
+ }
}