-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #932 from carstingaxion/fix/unit-test-topics
New and 100% unit-tests for Topic class
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
test/unit/php/includes/core/classes/class-test-topic.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Class handles unit tests for GatherPress\Core\Topic. | ||
* | ||
* @package GatherPress\Core | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace GatherPress\Tests\Core; | ||
|
||
use GatherPress\Core\Topic; | ||
use PMC\Unit_Test\Base; | ||
|
||
/** | ||
* Class Test_Topic. | ||
* | ||
* @coversDefaultClass \GatherPress\Core\Topic | ||
*/ | ||
class Test_Topic extends Base { | ||
/** | ||
* Coverage for __construct and setup_hooks. | ||
* | ||
* @covers ::__construct | ||
* @covers ::setup_hooks | ||
* | ||
* @return void | ||
*/ | ||
public function test_setup_hooks(): void { | ||
$instance = Topic::get_instance(); | ||
$hooks = array( | ||
array( | ||
'type' => 'action', | ||
'name' => 'init', | ||
'priority' => 10, | ||
'callback' => array( $instance, 'register_taxonomy' ), | ||
), | ||
); | ||
|
||
$this->assert_hooks( $hooks, $instance ); | ||
} | ||
|
||
/** | ||
* Coverage for register_taxonomy method. | ||
* | ||
* @covers ::register_taxonomy | ||
* | ||
* @return void | ||
*/ | ||
public function test_register_taxonomy(): void { | ||
$instance = Topic::get_instance(); | ||
|
||
unregister_taxonomy( Topic::TAXONOMY ); | ||
|
||
$this->assertFalse( taxonomy_exists( Topic::TAXONOMY ), 'Failed to assert that taxonomy does not exist.' ); | ||
|
||
$instance->register_taxonomy(); | ||
|
||
$this->assertTrue( taxonomy_exists( Topic::TAXONOMY ), 'Failed to assert that taxonomy exists.' ); | ||
} | ||
|
||
/** | ||
* Coverage for get_localized_taxonomy_slug method. | ||
* | ||
* @covers ::get_localized_taxonomy_slug | ||
* | ||
* @return void | ||
*/ | ||
public function test_get_localized_taxonomy_slug(): void { | ||
$instance = Topic::get_instance(); | ||
|
||
$this->assertSame( | ||
'topic', | ||
$instance->get_localized_taxonomy_slug(), | ||
'Failed to assert english taxonomy slug is "topic".' | ||
); | ||
|
||
$filter = static function ( string $translation, string $text, string $context ): string { | ||
if ( 'topic' !== $text || 'Taxonomy Slug' !== $context ) { | ||
return $translation; | ||
} | ||
return 'Ünit Tést'; | ||
}; | ||
|
||
/** | ||
* Instead of loading additional languages into the unit test suite, | ||
* we just filter the translated value, to mock different languages. | ||
* | ||
* Filters text with its translation based on context information for a domain. | ||
* | ||
* @param string $translation Translated text. | ||
* @param string $text Text to translate. | ||
* @param string $context Context information for the translators. | ||
* @return string Translated text. | ||
*/ | ||
add_filter( 'gettext_with_context_gatherpress', $filter, 10, 3 ); | ||
|
||
$this->assertSame( | ||
'unit-test', | ||
$instance->get_localized_taxonomy_slug(), | ||
'Failed to assert taxonomy slug is "unit-test".' | ||
); | ||
|
||
remove_filter( 'gettext_with_context_gatherpress', $filter ); | ||
} | ||
} |