2017-11-10 15:23:02 +00:00
|
|
|
<?php
|
2017-11-15 21:07:44 +00:00
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
2017-11-10 15:23:02 +00:00
|
|
|
/**
|
|
|
|
* Class TestCollections
|
|
|
|
*
|
|
|
|
* @package Test_Tainacan
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sample test case.
|
|
|
|
*/
|
2017-12-04 18:20:49 +00:00
|
|
|
class Taxonomies extends TAINACAN_UnitTestCase {
|
2017-11-10 15:23:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Teste da insercao de uma taxonomia simples
|
|
|
|
*/
|
|
|
|
function test_add() {
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
2017-11-10 15:23:02 +00:00
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-10 15:23:02 +00:00
|
|
|
|
|
|
|
//retorna a taxonomia
|
2017-11-21 11:15:44 +00:00
|
|
|
$test = $Tainacan_Taxonomies->fetch($taxonomy->get_id());
|
2017-11-10 15:23:02 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $test->get_name(), 'genero' );
|
|
|
|
$this->assertEquals( $test->get_description(), 'tipos de musica' );
|
2017-12-01 16:06:35 +00:00
|
|
|
$this->assertEquals( $test->get_allow_insert(), 'yes' );
|
2017-11-11 22:25:54 +00:00
|
|
|
$this->assertEquals( taxonomy_exists( $test->get_db_identifier() ) , true );
|
2017-11-10 15:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_add_term_taxonomy(){
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
|
|
|
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
2017-11-10 15:23:02 +00:00
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'genero',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-10 15:23:02 +00:00
|
|
|
|
|
|
|
//retorna a taxonomia
|
2017-11-21 11:15:44 +00:00
|
|
|
$taxonomy_test = $Tainacan_Taxonomies->fetch($taxonomy->get_id());
|
2017-11-10 15:23:02 +00:00
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$term = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy_test->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
'user' => 56
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-10 15:23:02 +00:00
|
|
|
|
2017-11-10 16:39:27 +00:00
|
|
|
//retorna um objeto da classe Tainacan_Term
|
2018-03-05 16:06:01 +00:00
|
|
|
$test = $Tainacan_Terms->fetch($term->get_term_id(), $taxonomy_test);
|
2017-11-10 16:39:27 +00:00
|
|
|
$this->assertEquals( $test->get_name(), 'Rock' );
|
|
|
|
$this->assertEquals( $test->get_user(), 56 );
|
2017-11-10 15:23:02 +00:00
|
|
|
}
|
2018-04-10 17:13:20 +00:00
|
|
|
|
|
|
|
function test_terms_of_draft_taxonomy() {
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-04-11 14:26:46 +00:00
|
|
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
|
|
|
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
2018-04-10 17:13:20 +00:00
|
|
|
|
|
|
|
$terms = $Tainacan_Terms->fetch(['hide_empty' => false], $taxonomy->get_id());
|
|
|
|
|
|
|
|
$this->assertEquals(0, sizeof($terms), 'new auto draft taxonomy should return 0 terms');
|
|
|
|
|
|
|
|
$term = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$terms = $Tainacan_Terms->fetch(['hide_empty' => false], $taxonomy->get_id());
|
|
|
|
|
|
|
|
$this->assertEquals(1, sizeof($terms), 'you should be able to create a term even if the taxonomy is still auto-draft');
|
|
|
|
|
2018-11-13 20:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_term_exists() {
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
|
|
|
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
|
|
|
|
|
|
|
$term = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$parent = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Parent',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$child = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Child',
|
|
|
|
'parent' => $parent->get_id()
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse( $Tainacan_Terms->term_exists('Reggae', $taxonomy->get_db_identifier()) );
|
|
|
|
$this->assertTrue( $Tainacan_Terms->term_exists('Rock', $taxonomy->get_db_identifier()) );
|
|
|
|
|
|
|
|
//var_dump( $Tainacan_Terms->term_exists('Rock', $taxonomy->get_db_identifier(), 0, true) );
|
|
|
|
|
|
|
|
// test extreme case
|
|
|
|
|
|
|
|
$term_2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'test 123',
|
|
|
|
'parent' => $term->get_id()
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse( $Tainacan_Terms->term_exists('test 123', $taxonomy->get_db_identifier(), 0) ); // parent 0
|
|
|
|
$this->assertTrue( $Tainacan_Terms->term_exists('test 123', $taxonomy->get_db_identifier(), $term->get_id()) ); // spaces in between
|
|
|
|
|
|
|
|
// testing passing taxonomy object
|
|
|
|
$this->assertTrue( $Tainacan_Terms->term_exists('Rock', $taxonomy) );
|
|
|
|
|
|
|
|
// testing passing ID
|
|
|
|
$this->assertTrue( $Tainacan_Terms->term_exists('Rock', $taxonomy->get_id()) );
|
|
|
|
|
|
|
|
// testing via Taxonomy object
|
|
|
|
$this->assertTrue( $taxonomy->term_exists('Rock') );
|
|
|
|
|
|
|
|
// testing retrieving the term
|
|
|
|
$this->assertTrue( $taxonomy->term_exists('Rock', 0, true) instanceof \WP_Term );
|
|
|
|
$this->assertEquals( $term->get_id(), $taxonomy->term_exists('Rock', 0, true)->term_taxonomy_id );
|
|
|
|
|
|
|
|
// test parent
|
|
|
|
$this->assertTrue( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier()) ); // parent null
|
|
|
|
$this->assertFalse( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier(), 0) ); // parent 0
|
|
|
|
$this->assertTrue( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier(), $parent->get_id()) ); // parent
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_term_validation() {
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
|
|
|
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
|
|
|
|
|
|
|
$term = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$parent = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Parent',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$child = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Child',
|
|
|
|
'parent' => $parent->get_id()
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$newTerm = new \Tainacan\Entities\Term();
|
|
|
|
$newTerm->set_name('Child');
|
|
|
|
$newTerm->set_taxonomy($taxonomy->get_db_identifier());
|
|
|
|
|
|
|
|
$this->assertTrue( $newTerm->validate() );
|
|
|
|
|
|
|
|
$newTerm->set_parent($parent->get_id());
|
|
|
|
|
|
|
|
$this->assertFalse( $newTerm->validate(), 'term should not validate because it has a duplicate in the same level' );
|
|
|
|
|
|
|
|
$child->set_description('changed');
|
|
|
|
|
|
|
|
$this->assertTrue( $child->validate(), 'child should validate');
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-10 17:13:20 +00:00
|
|
|
}
|
2019-01-18 21:49:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group enabled
|
|
|
|
*/
|
|
|
|
function test_enabled_post_types(){
|
|
|
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
|
|
|
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'genero',
|
|
|
|
//'enabled_post_types ' => ['post']
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$taxonomy->set('enabled_post_types', ['post']);
|
|
|
|
|
|
|
|
$taxonomy = $Tainacan_Taxonomies->insert($taxonomy);
|
|
|
|
|
|
|
|
$pto = get_object_taxonomies('post');
|
|
|
|
$pages = get_object_taxonomies('page');
|
|
|
|
$this->assertContains($taxonomy->get_db_identifier(), $pto);
|
|
|
|
$this->assertNotContains($taxonomy->get_db_identifier(), $pages);
|
|
|
|
}
|
2017-11-10 15:23:02 +00:00
|
|
|
}
|