2018-01-08 17:33:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
2018-01-19 22:39:32 +00:00
|
|
|
/**
|
|
|
|
* @group api
|
|
|
|
*/
|
2018-01-08 17:33:47 +00:00
|
|
|
class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
|
|
|
|
|
|
|
|
public function test_create_term(){
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => '1genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
2018-01-25 13:47:09 +00:00
|
|
|
'POST', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms'
|
2018-01-08 17:33:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$term = json_encode([
|
|
|
|
'name' => 'Termo teste',
|
|
|
|
'user' => get_current_user_id()
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request->set_body($term);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals('Termo teste', $data['name']);
|
|
|
|
$this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']);
|
|
|
|
}
|
2018-01-15 19:47:27 +00:00
|
|
|
|
|
|
|
public function test_delete(){
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => '1genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$term = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
'user' => get_current_user_id(),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-03-05 16:06:01 +00:00
|
|
|
$request = new \WP_REST_Request('DELETE', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term->get_term_id());
|
2018-01-15 19:47:27 +00:00
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertTrue($data);
|
|
|
|
|
2018-03-05 16:06:01 +00:00
|
|
|
$term = get_term($term->get_term_id());
|
2018-01-15 19:47:27 +00:00
|
|
|
|
|
|
|
$this->assertNull($term);
|
|
|
|
}
|
2018-01-17 14:33:28 +00:00
|
|
|
|
|
|
|
public function test_update_term(){
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => '1genero',
|
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$term = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
'user' => get_current_user_id(),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$new_attributes = json_encode([
|
|
|
|
'name' => 'Trap'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
2018-03-05 16:06:01 +00:00
|
|
|
'PATCH', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term->get_term_id()
|
2018-01-17 14:33:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$request->set_body($new_attributes);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertNotEquals('Rock', $data['name']);
|
|
|
|
$this->assertEquals('Trap', $data['name']);
|
|
|
|
|
|
|
|
$this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']);
|
|
|
|
}
|
2018-01-18 12:45:31 +00:00
|
|
|
|
|
|
|
public function test_fetch_terms(){
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'Gender',
|
|
|
|
'description' => 'Music types',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Rock',
|
|
|
|
'user' => get_current_user_id(),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-18 13:38:31 +00:00
|
|
|
$term2 = $this->tainacan_entity_factory->create_entity(
|
2018-01-18 12:45:31 +00:00
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Trap',
|
|
|
|
'user' => get_current_user_id(),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
2018-01-25 13:47:09 +00:00
|
|
|
'GET', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms'
|
2018-01-18 12:45:31 +00:00
|
|
|
);
|
|
|
|
|
2018-02-01 16:51:38 +00:00
|
|
|
$request->set_query_params([
|
2018-02-16 12:58:55 +00:00
|
|
|
'hideempty' => false
|
2018-02-01 16:51:38 +00:00
|
|
|
]);
|
2018-01-18 12:45:31 +00:00
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2018-03-19 13:12:32 +00:00
|
|
|
$terms_name = [$data[0]['name'], $data[1]['name']];
|
2018-01-18 12:45:31 +00:00
|
|
|
|
2018-03-19 13:12:32 +00:00
|
|
|
$this->assertContains('Rock', $terms_name);
|
|
|
|
$this->assertContains('Trap', $terms_name);
|
2018-01-18 13:38:31 +00:00
|
|
|
|
|
|
|
#### FETCH A TERM ####
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
2018-03-05 16:06:01 +00:00
|
|
|
'GET', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term2->get_term_id()
|
2018-01-18 13:38:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals('Trap', $data['name']);
|
2018-01-18 12:45:31 +00:00
|
|
|
}
|
2018-01-08 17:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|