From 01e0adb3fb943c527843108fc58538f584cb2e8b Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Sun, 30 Oct 2022 20:35:54 +0530 Subject: [PATCH 1/4] add PHPUnit tests --- phpunit.xml | 2 +- tests/phpunit/test-tools/Utils_Tests.php | 84 ++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 2e1d5405..b52920e8 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,7 +2,7 @@ bootstrap="tests/bootstrap.php" backupGlobals="false" processIsolation="false" - colors="false"> + colors="true"> ./tests/phpunit diff --git a/tests/phpunit/test-tools/Utils_Tests.php b/tests/phpunit/test-tools/Utils_Tests.php index d55145bd..f1342b80 100644 --- a/tests/phpunit/test-tools/Utils_Tests.php +++ b/tests/phpunit/test-tools/Utils_Tests.php @@ -45,4 +45,88 @@ public function data_provider_embedded_images() { ), ); } + + public function test_get_primary_category__default_category__yoast_deactivated() { + $term_array = array( + (object) array( + 'term_id' => 10, + 'name' => 'Uncategorized', + ) + ); + + \WP_Mock::userFunction( 'wp_get_post_terms', array( + 'times' => 1, + 'args' => array( null, 'category' ), + 'return' => $term_array, + ) ); + + $term_name = get_primary_category(); + + $this->assertEquals( $term_name, 'Uncategorized' ); + } + + public function test_get_primary_category__multiple_categories__yoast_deactivated() { + $term_array = array( + (object) array( + 'term_id' => 10, + 'name' => 'Science', + ), + (object) array( + 'term_id' => 10, + 'name' => 'Uncategorized', + ) + ); + + \WP_Mock::userFunction( 'wp_get_post_terms', array( + 'times' => 1, + 'args' => array( null, 'category' ), + 'return' => $term_array, + ) ); + + $term_name = get_primary_category(); + + $this->assertEquals( $term_name, 'Science' ); + } + + public function test_get_primary_category__default_category__yoast_activated() { + \WP_Mock::userFunction( 'yoast_get_primary_term_id', array( + 'times' => 1, + 'args' => array( 'category', null ), + 'return' => 123, + ) ); + + \WP_Mock::userFunction( 'get_term', array( + 'times' => 1, + 'args' => array( 123 ), + 'return' => (object) array( + 'term_id' => 123, + 'name' => 'Uncategorized', + ), + ) ); + + $term_name = get_primary_category(); + + $this->assertEquals( $term_name, 'Uncategorized' ); + } + + public function test_get_primary_category__custom_category__yoast_activated() { + \WP_Mock::userFunction( 'yoast_get_primary_term_id', array( + 'times' => 1, + 'args' => array( 'category', null ), + 'return' => 123, + ) ); + + \WP_Mock::userFunction( 'get_term', array( + 'times' => 1, + 'args' => array( 123 ), + 'return' => (object) array( + 'term_id' => 123, + 'name' => 'Art', + ), + ) ); + + $term_name = get_primary_category(); + + $this->assertEquals( $term_name, 'Art' ); + } } From a89b50ba825bbed10b1becd53230ba7a03fa0b6d Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Sun, 30 Oct 2022 20:36:10 +0530 Subject: [PATCH 2/4] add util to fetch primary category --- includes/functions/content-sync.php | 2 +- includes/functions/utils.php | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/includes/functions/content-sync.php b/includes/functions/content-sync.php index 9920d103..999ab1af 100644 --- a/includes/functions/content-sync.php +++ b/includes/functions/content-sync.php @@ -276,7 +276,7 @@ function get_post_data( $post ) { 'plainText' => wp_strip_all_tags( $content ), 'size' => str_word_count( wp_strip_all_tags( $content ) ), 'allSections' => Utils\get_post_categories_paths( $post->ID ), - 'sectionNames' => Utils\get_post_categories( $post->ID ), + 'sectionNames' => Utils\get_primary_category( $post->ID ), 'modifiedAt' => gmdate( \DateTime::RFC3339, strtotime( $post->post_modified_gmt ) ), 'tags' => Utils\get_post_tags( $post ), 'url' => $permalink, diff --git a/includes/functions/utils.php b/includes/functions/utils.php index 375954ee..86b43851 100644 --- a/includes/functions/utils.php +++ b/includes/functions/utils.php @@ -545,3 +545,28 @@ function get_post_categories( $post_id ) { function get_wp_sophi_versions() { return 'wp-' . get_bloginfo( 'version' ) . ':plugin-' . SOPHI_WP_VERSION; } + +/** + * Get the primary term name. + * + * @param string $taxonomy Optional. The taxonomy to get the primary term ID for. Defaults to category. + * @param int $post_id Optional. Post to get the primary term ID for. + * + * @return string + */ +function get_primary_category( $post_id = 0, $taxonomy = 'category' ) { + if ( ! function_exists( 'yoast_get_primary_term_id' ) ) { + $post_terms = wp_get_post_terms( $post_id, $taxonomy ); + + if ( is_array( $post_terms ) && count( $post_terms ) > 0 ) { + return $post_terms[0]->name; + } else { + return ''; + } + } + + $primary_term_id = yoast_get_primary_term_id( $taxonomy, $post_id ); + $primary_category = get_term( $primary_term_id ); + + return $primary_category->name; +} From 20522d3ecea761c6baa854fa63b8d5aec02e4e07 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Mon, 31 Oct 2022 11:28:50 +0530 Subject: [PATCH 3/4] replace string with array --- includes/functions/content-sync.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/functions/content-sync.php b/includes/functions/content-sync.php index 999ab1af..9a3442c3 100644 --- a/includes/functions/content-sync.php +++ b/includes/functions/content-sync.php @@ -276,7 +276,7 @@ function get_post_data( $post ) { 'plainText' => wp_strip_all_tags( $content ), 'size' => str_word_count( wp_strip_all_tags( $content ) ), 'allSections' => Utils\get_post_categories_paths( $post->ID ), - 'sectionNames' => Utils\get_primary_category( $post->ID ), + 'sectionNames' => array( Utils\get_primary_category( $post->ID ) ), 'modifiedAt' => gmdate( \DateTime::RFC3339, strtotime( $post->post_modified_gmt ) ), 'tags' => Utils\get_post_tags( $post ), 'url' => $permalink, From d3032f635a74a862bb5543dabb5c1fd8f18736af Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Tue, 1 Nov 2022 01:30:29 +0530 Subject: [PATCH 4/4] fix test --- tests/phpunit/test-tools/Utils_Tests.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/phpunit/test-tools/Utils_Tests.php b/tests/phpunit/test-tools/Utils_Tests.php index f1342b80..fc606f77 100644 --- a/tests/phpunit/test-tools/Utils_Tests.php +++ b/tests/phpunit/test-tools/Utils_Tests.php @@ -108,25 +108,4 @@ public function test_get_primary_category__default_category__yoast_activated() { $this->assertEquals( $term_name, 'Uncategorized' ); } - - public function test_get_primary_category__custom_category__yoast_activated() { - \WP_Mock::userFunction( 'yoast_get_primary_term_id', array( - 'times' => 1, - 'args' => array( 'category', null ), - 'return' => 123, - ) ); - - \WP_Mock::userFunction( 'get_term', array( - 'times' => 1, - 'args' => array( 123 ), - 'return' => (object) array( - 'term_id' => 123, - 'name' => 'Art', - ), - ) ); - - $term_name = get_primary_category(); - - $this->assertEquals( $term_name, 'Art' ); - } }