Merge pull request #542 from tainacan/feature/459

Adds option to replace item's document through CSV import
This commit is contained in:
Vinícius Nunes Medeiros 2021-05-17 09:17:14 -03:00 committed by GitHub
commit 6369a8c230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 950 additions and 1097 deletions

View File

@ -103,7 +103,6 @@ class Media {
* @return string the file path
*/
public function save_remote_file($url) {
set_time_limit(0);
$filename = tempnam(sys_get_temp_dir(), basename($url));
@ -134,7 +133,7 @@ class Media {
# Assign a callback function to the CURL Write-Function
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback);
# Exceute the download - note we DO NOT put the result into a variable!
# Execute the download - note we DO NOT put the result into a variable!
curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);

View File

@ -36,12 +36,14 @@ class CSV extends Importer {
}
$columns = [];
if ($rawColumns) {
foreach( $rawColumns as $index => $rawColumn ) {
if( strpos($rawColumn,'special_') === 0 ) {
if( $rawColumn === 'special_document' ) {
$this->set_option('document_index', $index);
} else if ($rawColumn === 'special_document|REPLACE') {
$this->set_option('document_import_mode', 'replace');
$this->set_option('document_index', $index);
} else if( $rawColumn === 'special_attachments' ||
$rawColumn === 'special_attachments|APPEND' ||
$rawColumn === 'special_attachments|REPLACE' ) {
@ -515,6 +517,11 @@ class CSV extends Importer {
} else if( strpos($column_value,'file:') === 0 ) {
$correct_value = trim(substr($column_value, 5));
if (isset(parse_url($correct_value)['scheme'] )) {
if ($this->get_option('document_import_mode') === 'replace' ) {
$this->add_log('Item Document will be replaced ... ');
$this->delete_previous_document_imgs($item_inserted->get_id(), $item_inserted->get_document());
$this->add_log('Deleted previous Item Documents ... ');
}
$id = $TainacanMedia->insert_attachment_from_url($correct_value, $item_inserted->get_id());
if(!$id){
@ -571,15 +578,7 @@ class CSV extends Importer {
break;
case 'REPLACE':
$this->add_log('Attachment REPLACE file ');
$args['post_parent'] = $item_inserted->get_id();
$args['post_type'] = 'attachment';
$args['post_status'] = 'any';
$args['post__not_in'] = [$item_inserted->get_document()];
$posts_query = new \WP_Query();
$query_result = $posts_query->query( $args );
foreach ( $query_result as $post ) {
wp_delete_attachment( $post->ID, true );
}
$this->delete_previous_document_imgs($item_inserted->get_id(), $item_inserted->get_document());
break;
}
@ -681,7 +680,7 @@ class CSV extends Importer {
* its value or values
* @param integer $collection_index The index in the $this->collections array of the collection the item is being inserted into
*
* @return Tainacan\Entities\Item Item inserted
* @return bool|Tainacan\Entities\Item Item inserted
*/
public function insert( $processed_item, $collection_index ) {
remove_action( 'post_updated', 'wp_save_post_revision' );
@ -916,10 +915,9 @@ class CSV extends Importer {
* @param $metadatum the metadata
* @param $values the categories names
*
* @return array empty with no category or array with IDs
* @return bool|array empty with no category or array with IDs
*/
private function insert_hierarchy( $metadatum, $values ) {
if (empty($values)) {
return false;
}
@ -982,14 +980,13 @@ class CSV extends Importer {
/**
* @param $collection_id
*
* @return array/bool false if has no mapping or associated array with metadata id and header
* @return array|bool false if has no mapping or associated array with metadata id and header
*/
public function get_mapping( $collection_id ){
$mapping = get_post_meta( $collection_id, 'metadata_mapping', true );
return ( $mapping ) ? $mapping : false;
return $mapping ?: false;
}
/**
* @inheritdoc
*
@ -1081,4 +1078,19 @@ class CSV extends Importer {
return $message;
}
private function delete_previous_document_imgs($item_id, $item_document) {
$previous_imgs = [
'post_parent' => $item_id,
'post_type' => 'attachment',
'post_status' => 'any',
'post__not_in' => [$item_document]
];
$posts_query = new \WP_Query();
$attachs = $posts_query->query($previous_imgs);
foreach ($attachs as $att) {
$this->add_log( "Deleting attachment [". $att->ID . "] " . $att->post_title);
wp_delete_attachment($att->ID, true);
}
}
}

View File

@ -59,7 +59,7 @@ abstract class Importer {
private $options = [];
/**
* Stores the default options for the importer options
* Stores default options for importer options
* @var array
*/
protected $default_options = [];
@ -114,7 +114,7 @@ abstract class Importer {
private $error_log = [];
/**
* Wether to abort importer execution.
* Whether to abort importer execution.
* @var bool
*/
private $abort = false;
@ -137,7 +137,6 @@ abstract class Importer {
];
public function __construct($attributess = array()) {
$this->id = uniqid();
$author = get_current_user_id();
@ -153,8 +152,6 @@ abstract class Importer {
}
}
}
}
public function _to_Array($short = false) {
@ -193,8 +190,7 @@ abstract class Importer {
* @param $url string
* @return bool
*/
public function set_url($url)
{
public function set_url($url) {
if(!empty($url) && !is_array($url))
{
$this->url = rtrim(trim($url), "/");
@ -207,8 +203,7 @@ abstract class Importer {
/**
* @return string or bool
*/
public function get_url()
{
public function get_url() {
if(!empty($this->url))
{
return $this->url;
@ -293,7 +288,6 @@ abstract class Importer {
$this->default_options = $options;
}
public function set_steps($steps) {
$this->steps = $steps;
}
@ -302,7 +296,6 @@ abstract class Importer {
return $this->steps;
}
private function get_transients() {
return $this->transients;
}
@ -559,7 +552,6 @@ abstract class Importer {
$step = $steps[$current_step];
if ($step['callback'] == 'process_collections') {
$totalItems = 0;
$currentItem = $this->get_current_collection_item();
$current_collection = $this->get_current_collection();
@ -577,18 +569,12 @@ abstract class Importer {
if ($totalItems > 0) {
$value = round( ($currentItem/$totalItems) * 100 );
}
} else {
if ( isset($step['total']) && is_numeric($step['total']) && $step['total'] > 0 ) {
$current = $this->get_in_step_count();
$value = round( ($current/$step['total']) * 100 );
}
}
}
return $value;
}

View File

@ -42,11 +42,10 @@ class Oaipmh_Importer extends Importer {
$this->remove_import_method('file');
$this->add_import_method('url');
}
/**
* Method implemented by the child importer class to proccess each item
* Method implemented by child importer class to proccess each item
* @return int
*/
public function process_item( $index, $collection_id ){
@ -55,7 +54,6 @@ class Oaipmh_Importer extends Importer {
$record_processed = [];
if ($index === 0) {
if( $collection_id['source_id'] !== 'sets' && $this->has_sets ){
$info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $collection_id['source_id'] );
$this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $collection_id['source_id']);
@ -63,7 +61,6 @@ class Oaipmh_Importer extends Importer {
$info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc" );
$this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc");
}
} else {
$token = $this->get_transient('resumptionToken');
$info = $this->requester( $this->get_url() . "?verb=ListRecords&resumptionToken=" . $token);
@ -75,7 +72,6 @@ class Oaipmh_Importer extends Importer {
return false;
}
try {
$xml = new \SimpleXMLElement($info['body']);

View File

@ -6,7 +6,6 @@ use \Tainacan\Entities;
class Old_Tainacan extends Importer{
protected $steps = [
[
'name' => 'Create Taxonomies',
'progress_label' => 'Creating taxonomies',
@ -33,7 +32,6 @@ class Old_Tainacan extends Importer{
'callback' => 'link_relationships',
'total' => 5
]
];
protected $tainacan_api_address, $wordpress_api_address, $actual_collection;
@ -64,7 +62,6 @@ class Old_Tainacan extends Importer{
*
*/
public function create_taxonomies() {
if (!$this->get_url()) {
$this->add_error_log('Site url not found');
$this->abort();
@ -74,7 +71,6 @@ class Old_Tainacan extends Importer{
$this->add_log('Creating taxonomies');
foreach ($this->get_taxonomies() as $taxonomy) {
$tax = new Entities\Taxonomy();
$tax->set_name( $taxonomy->name );
$tax->set_description( $taxonomy->description );
@ -85,20 +81,16 @@ class Old_Tainacan extends Importer{
$tax = $this->tax_repo->insert($tax);
$this->add_log('Taxonomy ' . $tax->get_name() . ' created, id from Old'. $taxonomy->term_id );
$this->add_transient('tax_' . $taxonomy->term_id . '_id', $tax->get_id());
$this->add_transient('tax_' . $taxonomy->term_id . '_name', $tax->get_name());
if (isset($taxonomy->children) && $tax) {
$this->add_all_terms($tax, $taxonomy->children);
}
} else {
$this->add_log('Error creating taxonomy ' . $taxonomy->name );
$this->add_log($tax->get_errors());
}
}
return false;
@ -109,17 +101,14 @@ class Old_Tainacan extends Importer{
*
*/
public function create_repo_metadata() {
$this->add_log('Creating repository metadata');
foreach ($this->get_repo_metadata() as $metadata) {
if (isset($metadata->slug) && strpos($metadata->slug, 'socialdb_property_fixed') === false) {
$metadatum_id = $this->create_metadata( $metadata );
} elseif ( strpos($metadata->slug, 'socialdb_property_fixed_tags') !== false ) {
$metadatum_id = $this->create_metadata( $metadata );
}
}
$this->add_log('FInished repository metadata');

View File

@ -316,11 +316,9 @@ class Test_Importer extends Importer {
</div>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
@ -334,7 +332,6 @@ class Test_Importer extends Importer {
if ($tax1->validate()) {
$tax1 = $this->tax_repo->insert($tax1);
} else {
/**
* In these set up steps, if we have an error adding
* a taxonomy, collection or metadatum, there is no point
@ -346,13 +343,11 @@ class Test_Importer extends Importer {
$this->add_error_log($tax1->get_errors());
$this->abort();
return false;
}
$this->add_transient('tax_1_id', $tax1->get_id());
return false;
}
public function create_collections() {
@ -367,10 +362,8 @@ class Test_Importer extends Importer {
$this->add_error_log($col1->get_errors());
$this->abort();
return false;
}
$col1_map = [];
// metadata
@ -697,7 +690,6 @@ class Test_Importer extends Importer {
*
*/
private function create_metadata( $args, $collection ) {
$metadatum = new Entities\Metadatum();
$metadatum->set_name($args['name']);
$metadatum->set_collection($collection);
@ -758,7 +750,6 @@ class Test_Importer extends Importer {
return $array;
}
public function get_col2_item($index) {
return [
'field1' => 'Collection 2 item ' . $index,
@ -767,6 +758,4 @@ class Test_Importer extends Importer {
];
}
}

View File

@ -20,7 +20,7 @@
@click="isMenuCompressed = !isMenuCompressed">
<span
v-tooltip="{
content: $i18n.get('label_shrink_menu'),
content: isMenuCompressed ? $i18n.get('label_expand_menu') : $i18n.get('label_shrink_menu'),
autoHide: true,
placement: 'auto-end',
classes: ['tooltip', 'repository-tooltip']
@ -28,7 +28,8 @@
class="icon">
<i
:class="{ 'tainacan-icon-arrowleft' : !isMenuCompressed, 'tainacan-icon-arrowright' : isMenuCompressed }"
class="tainacan-icon tainacan-icon-1-25em"/>
class="tainacan-icon tainacan-icon-1-25em"
/>
</span>
</button>
<tainacan-header />

View File

@ -474,6 +474,7 @@ return apply_filters( 'tainacan-admin-i18n', [
'label_page' => __( 'Page', 'tainacan' ),
'label_current_page' => __( 'Current page', 'tainacan' ),
'label_shrink_menu' => __( 'Shrink menu', 'tainacan' ),
'label_expand_menu' => __( 'Expand menu', 'tainacan' ),
'label_document_uploaded' => __( 'Document uploaded', 'tainacan' ),
/* translators: Filter of the repository, not a repository of filter! */
'label_repository_filter' => __( 'Repository filter', 'tainacan' ),

View File

@ -536,5 +536,3 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
} */// TODO automate test this
}
?>

View File

@ -8,9 +8,7 @@ namespace Tainacan\Tests;
class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
public function test_create() {
$params = json_encode([
'importer_slug' => 'csv'
]);
$params = json_encode([ 'importer_slug' => 'csv' ]);
$request = new \WP_REST_Request('POST', $this->namespace . '/importers/session');
$request->set_body($params);
@ -55,5 +53,3 @@ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
}
}
?>

View File

@ -64,5 +64,3 @@ class TAINACAN_REST_Logs_Controller extends TAINACAN_UnitApiTestCase {
}
}
?>

View File

@ -952,5 +952,3 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
}
}
?>

View File

@ -8,23 +8,14 @@ namespace Tainacan\Tests;
class TAINACAN_REST_Metadata_Types_Controller extends TAINACAN_UnitApiTestCase {
public function test_get_metadata_types(){
$ftype_request = new \WP_REST_Request('GET', $this->namespace . '/metadata-types');
$ftype_response = $this->server->dispatch($ftype_request);
$data = $ftype_response->get_data();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$metadata_types = $Tainacan_Metadata->fetch_metadata_types('NAME');
$this->assertEquals(count($metadata_types), count($data));
foreach ($data as $ftype){
$this->assertContains($ftype['name'], $metadata_types);
}
}
}
?>

View File

@ -7,8 +7,6 @@ namespace Tainacan\Tests;
*/
class TAINACAN_REST_Private_Files extends TAINACAN_UnitApiTestCase {
public function test_create_item() {
$orig_file = './tests/attachment/codeispoetrywp.jpg';
@ -62,12 +60,9 @@ class TAINACAN_REST_Private_Files extends TAINACAN_UnitApiTestCase {
$folder = 'tainacan-items/' . $this->collection->get_id() . '/' . $this->item->get_id();
$this->assertContains( $folder, $attachment_data['file'] );
}
function test_internal_api() {
$orig_file = './tests/attachment/codeispoetrywp.jpg';
$this->test_file = '/tmp/codeispoetrywp.jpg';
copy( $orig_file, $this->test_file );
@ -94,15 +89,10 @@ class TAINACAN_REST_Private_Files extends TAINACAN_UnitApiTestCase {
);
$attachment_id = \Tainacan\Media::get_instance()->insert_attachment_from_file($this->test_file, $this->item->get_id());
$attachment_data = wp_get_attachment_metadata($attachment_id);
$folder = 'tainacan-items/' . $this->collection->get_id() . '/' . $this->item->get_id();
$this->assertContains( $folder, $attachment_data['file'] );
}
}
?>

View File

@ -8,9 +8,7 @@ namespace Tainacan\Tests;
class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
public function test_queries(){
// Populate the database
$collectionB = $this->tainacan_entity_factory->create_entity(
'collection',
[
@ -57,8 +55,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
),
true
);
// Create Term
// Create Term
$termA = $this->tainacan_entity_factory->create_entity(
'term',
array(
@ -173,8 +171,6 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$this->assertCount(1, $data1);
$this->assertEquals($collectionB->get_name(), $data1[0]['name']);
// Search collection with a specific keyword and not other keyword
$search_query = ['search' => 'Collection -A'];
@ -190,14 +186,9 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$names = [$data2[0]['name'], $data2[1]['name']];
$this->assertNotContains('A', $names);
/* Meta Query:
*
* Fetch items from a collection desc ordered by metadatumA1 and its only in range A to F.
*
* */
$meta_query = [
'metakey' => $metadatumA1->get_id(),
'orderby' => 'meta_value',
@ -212,16 +203,11 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
];
$meta_query_request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items');
$meta_query_request->set_query_params($meta_query);
$meta_query_response = $this->server->dispatch($meta_query_request);
$data3 = $meta_query_response->get_data()['items'];
$this->assertCount(2, $data3);
$metadatumA1_slug = $metadatumA1->get_slug();
$values = [$data3[0]['metadata'][$metadatumA1_slug]['value'], $data3[1]['metadata'][$metadatumA1_slug]['value']];
$this->assertNotContains('G', $values);
@ -230,13 +216,9 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$this->assertEquals('E', $data3[0]['metadata'][$metadatumA1_slug]['value']);
$this->assertEquals('D', $data3[1]['metadata'][$metadatumA1_slug]['value']);
/* Date Query:
*
* Fetch posts for today
*
* */
$today = getdate();
$date_query = [
@ -268,11 +250,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$this->assertCount(0, $data5);
/* Tax Query
*
* Fetch items under a taxonomy with a specific term
*
* */
$tax_query = [
'taxquery' => [
[
@ -301,5 +280,3 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$this->assertNotContains('Item A-3', $itemsA1_A2);
}
}
?>

View File

@ -7,8 +7,6 @@ namespace Tainacan\Tests;
*
*/
class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
public function setUp() {
parent::setUp();
// reset WP_Roles object. Possible bug was cleaning database between tests, but not the object
@ -35,7 +33,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$data = $name_response->get_data();
$this->assertArrayHasKey('tainacan-new-role', $data);
$this->assertEquals('New role', $data['tainacan-new-role']['name']);
}
public function test_create_remove_roles() {
@ -58,13 +55,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$name_response = $this->server->dispatch($request);
$data = $name_response->get_data();
$this->assertArrayNotHasKey('tainacan-super-role', $data);
}
public function test_edit_role() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
$request->set_query_params(['name' => 'New role']);
@ -92,29 +85,24 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('Changed name', $role['name']);
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
$request->set_query_params(
[
'add_cap' => 'manage_options'
]
);
$response = $this->server->dispatch($request);
$this->assertEquals( 400, $response->get_status() );
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
$request->set_query_params(
[
'add_cap' => 'manage_tainacan_collection_234'
]
);
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
}
public function test_edit_role_validation() {
@ -171,33 +159,27 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
}
public function test_get_role() {
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/administrator');
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( translate_user_role('Administrator'), $data['name'] );
$this->assertArrayHasKey('manage_tainacan', $data['capabilities']);
$this->assertTrue($data['capabilities']['manage_tainacan']);
}
public function test_get_roles() {
$request = new \WP_REST_Request('GET', $this->namespace . '/roles');
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
foreach (\tainacan_roles()->get_tainacan_roles() as $role => $r) {
$this->assertArrayHasKey($role, $data);
}
@ -210,7 +192,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$request->set_query_params(['name' => 'New role']);
$create = $this->server->dispatch($request);
//var_dump($create);
$this->assertEquals( 201, $create->get_status() );
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
@ -267,7 +248,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
}
public function test_get_collection_caps() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
@ -290,9 +270,7 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$contributor->add_cap( 'tnc_col_all_edit_items' );
$contributor->add_cap( 'tnc_col_all_edit_published_items' );
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/capabilities');
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
@ -308,11 +286,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertArrayHasKey('contributor', $caps['tnc_col_' . $collection->get_id() . '_edit_published_items']['roles_inherited']);
$this->assertArrayHasKey('administrator', $caps['tnc_col_' . $collection->get_id() . '_delete_published_items']['roles_inherited']);
}
function test_get_repo_capabilities() {
$role = add_role('test', 'test', ['tnc_rep_edit_metadata'=>true]);
$request = new \WP_REST_Request('GET', $this->namespace . '/capabilities');
@ -329,12 +305,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertArrayNotHasKey('editor', $caps['manage_tainacan']['roles_inherited']);
$this->assertArrayNotHasKey('administrator', $caps['manage_tainacan']['roles_inherited']);
}
function test_edit_collection_users_permission() {
global $current_user;
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' ));
@ -349,7 +322,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
'add_cap' => 'tnc_col_12_edit_items'
]
);
$response = $this->server->dispatch($request);
$this->assertEquals( 403, $response->get_status(), 'should not be permitted');
@ -358,21 +330,17 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$current_user = $sub_user;
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
$request->set_query_params(
[
'name' => 'Changed name',
'add_cap' => 'tnc_col_12_edit_items'
]
);
$response = $this->server->dispatch($request);
$this->assertEquals( 403, $response->get_status(), 'should still not be permitted because edits name');
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
$request->set_query_params(
[
'add_cap' => 'tnc_rep_edit_metadata'
@ -383,9 +351,7 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals( 403, $response->get_status(), 'should not be permitted');
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
$request->set_query_params(
[
'add_cap' => 'tnc_col_12_edit_items'
@ -395,13 +361,8 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status(), 'should be permitted');
}
/**
* @group xis
*/
public function test_create_get_roles_with_caps() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
@ -418,7 +379,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals( 201, $create->get_status() );
$request = new \WP_REST_Request('GET', $this->namespace . '/roles');
//$request->set_query_params($name_query);
$name_response = $this->server->dispatch($request);
@ -430,13 +390,10 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertArrayHasKey('tnc_rep_edit_collections', $role['capabilities']);
$this->assertTrue($role['capabilities']['tnc_rep_edit_collections']);
}
public function test_edit_role_with_caps() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
$request->set_query_params([
'name' => 'New role',
'capabilities' => [
@ -468,8 +425,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertArrayHasKey('manage_tainacan_collection_123', $data['capabilities']);
$this->assertTrue($data['capabilities']['manage_tainacan_collection_123']);
// EDIT
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
@ -491,7 +446,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals( 200, $response->get_status() );
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/tainacan-new-role');
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
@ -510,27 +464,18 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertArrayNotHasKey('tnc_rep_edit_taxonomies', $data['capabilities']);
$this->assertArrayNotHasKey('manage_tainacan_collection_123', $data['capabilities']);
}
function test_new_role_can_read() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
$request->set_query_params(['name' => 'New role']);
$create = $this->server->dispatch($request);
$this->assertEquals( 201, $create->get_status() );
$role = get_role('tainacan-new-role');
$this->assertTrue( $role->has_cap( 'read' ) );
}
}
?>

View File

@ -3,17 +3,16 @@
namespace Tainacan\Tests;
/**
* Class TestCollections
* Class Taxonomies
*
* @package Test_Tainacan
*/
/**
* Sample test case.
* Taxonomies test case.
*/
class Taxonomies extends TAINACAN_UnitTestCase {
/**
* Teste da insercao de uma taxonomia simples
*/
@ -334,7 +333,6 @@ class Taxonomies extends TAINACAN_UnitTestCase {
}
function test_brackets_2() {
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
@ -411,9 +409,6 @@ class Taxonomies extends TAINACAN_UnitTestCase {
$itemMeta2_check = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum);
$this->assertEquals('[Rock]', $itemMeta2_check->get_value()->get_name());
}
function test_metadata_taxonomy_term_count() {
@ -538,7 +533,6 @@ class Taxonomies extends TAINACAN_UnitTestCase {
$itemMeta1_repo->validate();
$Tainacan_Item_Metadata->insert($itemMeta1_repo);
$i2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
@ -587,8 +581,6 @@ class Taxonomies extends TAINACAN_UnitTestCase {
function test_term_taxonomy_filtered_by_collections() {
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
@ -704,7 +696,6 @@ class Taxonomies extends TAINACAN_UnitTestCase {
$response = $matches[3][0];
$this->assertEquals($response, $expected_response);
$meta_query = [
'metaquery' => [
[

View File

@ -9,14 +9,11 @@ namespace Tainacan\Tests;
*/
/**
* Sample test case.
* TestUtilities
*/
class TestUtilities extends TAINACAN_UnitTestCase {
function test_initials() {
$string = 'Roberto Carlos';
$this->assertEquals('RC', tainacan_get_initials($string));
@ -42,7 +39,6 @@ class TestUtilities extends TAINACAN_UnitTestCase {
$string = '';
$this->assertEquals('', tainacan_get_initials($string));
}
function test_get_descendants_ids() {