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 * @return string the file path
*/ */
public function save_remote_file($url) { public function save_remote_file($url) {
set_time_limit(0); set_time_limit(0);
$filename = tempnam(sys_get_temp_dir(), basename($url)); $filename = tempnam(sys_get_temp_dir(), basename($url));
@ -134,7 +133,7 @@ class Media {
# Assign a callback function to the CURL Write-Function # Assign a callback function to the CURL Write-Function
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); 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); curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
$error_msg = curl_error($ch); $error_msg = curl_error($ch);

View File

@ -36,13 +36,15 @@ class CSV extends Importer {
} }
$columns = []; $columns = [];
if ($rawColumns) {
if( $rawColumns ) {
foreach( $rawColumns as $index => $rawColumn ) { foreach( $rawColumns as $index => $rawColumn ) {
if( strpos($rawColumn,'special_') === 0 ) { if( strpos($rawColumn,'special_') === 0 ) {
if( $rawColumn === 'special_document' ) { if( $rawColumn === 'special_document' ) {
$this->set_option('document_index', $index); $this->set_option('document_index', $index);
} else if( $rawColumn === 'special_attachments' || } 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|APPEND' ||
$rawColumn === 'special_attachments|REPLACE' ) { $rawColumn === 'special_attachments|REPLACE' ) {
$this->set_option('attachment_index', $index); $this->set_option('attachment_index', $index);
@ -514,7 +516,12 @@ class CSV extends Importer {
} }
} else if( strpos($column_value,'file:') === 0 ) { } else if( strpos($column_value,'file:') === 0 ) {
$correct_value = trim(substr($column_value, 5)); $correct_value = trim(substr($column_value, 5));
if( isset(parse_url($correct_value)['scheme'] ) ) { 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()); $id = $TainacanMedia->insert_attachment_from_url($correct_value, $item_inserted->get_id());
if(!$id){ if(!$id){
@ -571,15 +578,7 @@ class CSV extends Importer {
break; break;
case 'REPLACE': case 'REPLACE':
$this->add_log('Attachment REPLACE file '); $this->add_log('Attachment REPLACE file ');
$args['post_parent'] = $item_inserted->get_id(); $this->delete_previous_document_imgs($item_inserted->get_id(), $item_inserted->get_document());
$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 );
}
break; break;
} }
@ -624,10 +623,10 @@ class CSV extends Importer {
$line = substr($line, $cut_start); $line = substr($line, $cut_start);
} }
$end = substr($line, ( strlen($line) - strlen($this->get_option('enclosure')) ) , strlen($this->get_option('enclosure'))); $end = substr($line, ( strlen($line) - strlen($this->get_option('enclosure')) ) , strlen($this->get_option('enclosure')));
if( $this->get_option('enclosure') === $end ) { if( $this->get_option('enclosure') === $end ) {
$line = substr($line, 0, ( strlen($line) - strlen($this->get_option('enclosure')) ) ); $line = substr($line, 0, ( strlen($line) - strlen($this->get_option('enclosure')) ) );
} }
$delimiter = $this->get_option('enclosure').$this->get_option('delimiter').$this->get_option('enclosure'); $delimiter = $this->get_option('enclosure').$this->get_option('delimiter').$this->get_option('enclosure');
@ -681,7 +680,7 @@ class CSV extends Importer {
* its value or values * its value or values
* @param integer $collection_index The index in the $this->collections array of the collection the item is being inserted into * @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 ) { public function insert( $processed_item, $collection_index ) {
remove_action( 'post_updated', 'wp_save_post_revision' ); remove_action( 'post_updated', 'wp_save_post_revision' );
@ -916,10 +915,9 @@ class CSV extends Importer {
* @param $metadatum the metadata * @param $metadatum the metadata
* @param $values the categories names * @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 ){ private function insert_hierarchy( $metadatum, $values ) {
if (empty($values)) { if (empty($values)) {
return false; return false;
} }
@ -982,14 +980,13 @@ class CSV extends Importer {
/** /**
* @param $collection_id * @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 ){ public function get_mapping( $collection_id ){
$mapping = get_post_meta( $collection_id, 'metadata_mapping', true ); $mapping = get_post_meta( $collection_id, 'metadata_mapping', true );
return ( $mapping ) ? $mapping : false; return $mapping ?: false;
} }
/** /**
* @inheritdoc * @inheritdoc
* *
@ -1081,4 +1078,19 @@ class CSV extends Importer {
return $message; 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 = []; private $options = [];
/** /**
* Stores the default options for the importer options * Stores default options for importer options
* @var array * @var array
*/ */
protected $default_options = []; protected $default_options = [];
@ -114,7 +114,7 @@ abstract class Importer {
private $error_log = []; private $error_log = [];
/** /**
* Wether to abort importer execution. * Whether to abort importer execution.
* @var bool * @var bool
*/ */
private $abort = false; private $abort = false;
@ -137,7 +137,6 @@ abstract class Importer {
]; ];
public function __construct($attributess = array()) { public function __construct($attributess = array()) {
$this->id = uniqid(); $this->id = uniqid();
$author = get_current_user_id(); $author = get_current_user_id();
@ -153,8 +152,6 @@ abstract class Importer {
} }
} }
} }
} }
public function _to_Array($short = false) { public function _to_Array($short = false) {
@ -193,8 +190,7 @@ abstract class Importer {
* @param $url string * @param $url string
* @return bool * @return bool
*/ */
public function set_url($url) public function set_url($url) {
{
if(!empty($url) && !is_array($url)) if(!empty($url) && !is_array($url))
{ {
$this->url = rtrim(trim($url), "/"); $this->url = rtrim(trim($url), "/");
@ -207,8 +203,7 @@ abstract class Importer {
/** /**
* @return string or bool * @return string or bool
*/ */
public function get_url() public function get_url() {
{
if(!empty($this->url)) if(!empty($this->url))
{ {
return $this->url; return $this->url;
@ -293,7 +288,6 @@ abstract class Importer {
$this->default_options = $options; $this->default_options = $options;
} }
public function set_steps($steps) { public function set_steps($steps) {
$this->steps = $steps; $this->steps = $steps;
} }
@ -302,7 +296,6 @@ abstract class Importer {
return $this->steps; return $this->steps;
} }
private function get_transients() { private function get_transients() {
return $this->transients; return $this->transients;
} }
@ -559,7 +552,6 @@ abstract class Importer {
$step = $steps[$current_step]; $step = $steps[$current_step];
if ($step['callback'] == 'process_collections') { if ($step['callback'] == 'process_collections') {
$totalItems = 0; $totalItems = 0;
$currentItem = $this->get_current_collection_item(); $currentItem = $this->get_current_collection_item();
$current_collection = $this->get_current_collection(); $current_collection = $this->get_current_collection();
@ -577,18 +569,12 @@ abstract class Importer {
if ($totalItems > 0) { if ($totalItems > 0) {
$value = round( ($currentItem/$totalItems) * 100 ); $value = round( ($currentItem/$totalItems) * 100 );
} }
} else { } else {
if ( isset($step['total']) && is_numeric($step['total']) && $step['total'] > 0 ) { if ( isset($step['total']) && is_numeric($step['total']) && $step['total'] > 0 ) {
$current = $this->get_in_step_count(); $current = $this->get_in_step_count();
$value = round( ($current/$step['total']) * 100 ); $value = round( ($current/$step['total']) * 100 );
} }
} }
} }
return $value; return $value;
} }

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,6 @@ use \Tainacan\Entities;
class Old_Tainacan extends Importer{ class Old_Tainacan extends Importer{
protected $steps = [ protected $steps = [
[ [
'name' => 'Create Taxonomies', 'name' => 'Create Taxonomies',
'progress_label' => 'Creating taxonomies', 'progress_label' => 'Creating taxonomies',
@ -33,7 +32,6 @@ class Old_Tainacan extends Importer{
'callback' => 'link_relationships', 'callback' => 'link_relationships',
'total' => 5 'total' => 5
] ]
]; ];
protected $tainacan_api_address, $wordpress_api_address, $actual_collection; protected $tainacan_api_address, $wordpress_api_address, $actual_collection;
@ -64,8 +62,7 @@ class Old_Tainacan extends Importer{
* *
*/ */
public function create_taxonomies() { public function create_taxonomies() {
if (!$this->get_url()) {
if(!$this->get_url()){
$this->add_error_log('Site url not found'); $this->add_error_log('Site url not found');
$this->abort(); $this->abort();
return false; return false;
@ -74,57 +71,49 @@ class Old_Tainacan extends Importer{
$this->add_log('Creating taxonomies'); $this->add_log('Creating taxonomies');
foreach ($this->get_taxonomies() as $taxonomy) { foreach ($this->get_taxonomies() as $taxonomy) {
$tax = new Entities\Taxonomy();
$tax->set_name( $taxonomy->name );
$tax->set_description( $taxonomy->description );
$tax->set_allow_insert('yes');
$tax->set_status('publish');
$tax = new Entities\Taxonomy(); if ($tax->validate()) {
$tax->set_name( $taxonomy->name ); $tax = $this->tax_repo->insert($tax);
$tax->set_description( $taxonomy->description );
$tax->set_allow_insert('yes');
$tax->set_status('publish');
if ($tax->validate()) {
$tax = $this->tax_repo->insert($tax);
$this->add_log('Taxonomy ' . $tax->get_name() . ' created, id from Old'. $taxonomy->term_id ); $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());
$this->add_transient('tax_' . $taxonomy->term_id . '_id', $tax->get_id()); if (isset($taxonomy->children) && $tax) {
$this->add_transient('tax_' . $taxonomy->term_id . '_name', $tax->get_name()); $this->add_all_terms($tax, $taxonomy->children);
}
if (isset($taxonomy->children) && $tax) {
$this->add_all_terms($tax, $taxonomy->children);
}
} else { } else {
$this->add_log('Error creating taxonomy ' . $taxonomy->name ); $this->add_log('Error creating taxonomy ' . $taxonomy->name );
$this->add_log($tax->get_errors()); $this->add_log($tax->get_errors());
}
} }
}
return false; return false;
} }
/** /**
* create the repository metadata which each collection inherits by default * create the repository metadata which each collection inherits by default
* *
*/ */
public function create_repo_metadata(){ public function create_repo_metadata() {
$this->add_log('Creating repository metadata');
$this->add_log('Creating repository metadata');
foreach ($this->get_repo_metadata() as $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 );
}
}
if (isset($metadata->slug) && strpos($metadata->slug, 'socialdb_property_fixed') === false) { $this->add_log('FInished repository metadata');
$metadatum_id = $this->create_metadata( $metadata ); return false;
} elseif ( strpos($metadata->slug, 'socialdb_property_fixed_tags') !== false ){ }
$metadatum_id = $this->create_metadata( $metadata );
}
}
$this->add_log('FInished repository metadata');
return false;
}
/** /**
* create all collections and its metadata * create all collections and its metadata

View File

@ -316,11 +316,9 @@ class Test_Importer extends Importer {
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<?php <?php
return ob_get_clean(); return ob_get_clean();
} }
@ -334,7 +332,6 @@ class Test_Importer extends Importer {
if ($tax1->validate()) { if ($tax1->validate()) {
$tax1 = $this->tax_repo->insert($tax1); $tax1 = $this->tax_repo->insert($tax1);
} else { } else {
/** /**
* In these set up steps, if we have an error adding * In these set up steps, if we have an error adding
* a taxonomy, collection or metadatum, there is no point * a taxonomy, collection or metadatum, there is no point
@ -345,14 +342,12 @@ class Test_Importer extends Importer {
$this->add_error_log('Error creating taxonomy Color'); $this->add_error_log('Error creating taxonomy Color');
$this->add_error_log($tax1->get_errors()); $this->add_error_log($tax1->get_errors());
$this->abort(); $this->abort();
return false; return false;
} }
$this->add_transient('tax_1_id', $tax1->get_id()); $this->add_transient('tax_1_id', $tax1->get_id());
return false; return false;
} }
public function create_collections() { public function create_collections() {
@ -366,11 +361,9 @@ class Test_Importer extends Importer {
$this->add_error_log('Error creating Collection 1'); $this->add_error_log('Error creating Collection 1');
$this->add_error_log($col1->get_errors()); $this->add_error_log($col1->get_errors());
$this->abort(); $this->abort();
return false; return false;
} }
$col1_map = []; $col1_map = [];
// metadata // metadata
@ -696,8 +689,7 @@ class Test_Importer extends Importer {
* @param $collection * @param $collection
* *
*/ */
private function create_metadata( $args, $collection ){ private function create_metadata( $args, $collection ) {
$metadatum = new Entities\Metadatum(); $metadatum = new Entities\Metadatum();
$metadatum->set_name($args['name']); $metadatum->set_name($args['name']);
$metadatum->set_collection($collection); $metadatum->set_collection($collection);
@ -758,7 +750,6 @@ class Test_Importer extends Importer {
return $array; return $array;
} }
public function get_col2_item($index) { public function get_col2_item($index) {
return [ return [
'field1' => 'Collection 2 item ' . $index, 'field1' => 'Collection 2 item ' . $index,
@ -766,7 +757,5 @@ class Test_Importer extends Importer {
'field3' => 'Collection 2 whatever ' . $index, 'field3' => 'Collection 2 whatever ' . $index,
]; ];
} }
} }

View File

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

View File

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

View File

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

View File

@ -7,10 +7,8 @@ namespace Tainacan\Tests;
*/ */
class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase { class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
public function test_create(){ public function test_create() {
$params = json_encode([ $params = json_encode([ 'importer_slug' => 'csv' ]);
'importer_slug' => 'csv'
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/importers/session'); $request = new \WP_REST_Request('POST', $this->namespace . '/importers/session');
$request->set_body($params); $request->set_body($params);
@ -54,6 +52,4 @@ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(1234, $__importer->get_collections()[0]['total_items']); $this->assertEquals(1234, $__importer->get_collections()[0]['total_items']);
} }
} }
?>

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

View File

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

View File

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

View File

@ -7,8 +7,6 @@ namespace Tainacan\Tests;
* *
*/ */
class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
// reset WP_Roles object. Possible bug was cleaning database between tests, but not the object // 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(); $data = $name_response->get_data();
$this->assertArrayHasKey('tainacan-new-role', $data); $this->assertArrayHasKey('tainacan-new-role', $data);
$this->assertEquals('New role', $data['tainacan-new-role']['name']); $this->assertEquals('New role', $data['tainacan-new-role']['name']);
} }
public function test_create_remove_roles() { public function test_create_remove_roles() {
@ -58,13 +55,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$name_response = $this->server->dispatch($request); $name_response = $this->server->dispatch($request);
$data = $name_response->get_data(); $data = $name_response->get_data();
$this->assertArrayNotHasKey('tainacan-super-role', $data); $this->assertArrayNotHasKey('tainacan-super-role', $data);
} }
public function test_edit_role() { public function test_edit_role() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles'); $request = new \WP_REST_Request('POST', $this->namespace . '/roles');
$request->set_query_params(['name' => 'New role']); $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']); $this->assertEquals('Changed name', $role['name']);
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
$request->set_query_params( $request->set_query_params(
[ [
'add_cap' => 'manage_options' 'add_cap' => 'manage_options'
] ]
); );
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 400, $response->get_status() ); $this->assertEquals( 400, $response->get_status() );
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
$request->set_query_params( $request->set_query_params(
[ [
'add_cap' => 'manage_tainacan_collection_234' 'add_cap' => 'manage_tainacan_collection_234'
] ]
); );
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
} }
public function test_edit_role_validation() { public function test_edit_role_validation() {
@ -171,33 +159,27 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
} }
public function test_get_role() { public function test_get_role() {
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/administrator'); $request = new \WP_REST_Request('GET', $this->namespace . '/roles/administrator');
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( translate_user_role('Administrator'), $data['name'] ); $this->assertEquals( translate_user_role('Administrator'), $data['name'] );
$this->assertArrayHasKey('manage_tainacan', $data['capabilities']); $this->assertArrayHasKey('manage_tainacan', $data['capabilities']);
$this->assertTrue($data['capabilities']['manage_tainacan']); $this->assertTrue($data['capabilities']['manage_tainacan']);
} }
public function test_get_roles() { public function test_get_roles() {
$request = new \WP_REST_Request('GET', $this->namespace . '/roles'); $request = new \WP_REST_Request('GET', $this->namespace . '/roles');
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$data = $response->get_data(); $data = $response->get_data();
foreach (\tainacan_roles()->get_tainacan_roles() as $role => $r) { foreach (\tainacan_roles()->get_tainacan_roles() as $role => $r) {
$this->assertArrayHasKey($role, $data); $this->assertArrayHasKey($role, $data);
} }
@ -210,7 +192,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$request->set_query_params(['name' => 'New role']); $request->set_query_params(['name' => 'New role']);
$create = $this->server->dispatch($request); $create = $this->server->dispatch($request);
//var_dump($create);
$this->assertEquals( 201, $create->get_status() ); $this->assertEquals( 201, $create->get_status() );
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); $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() { public function test_get_collection_caps() {
$collection = $this->tainacan_entity_factory->create_entity( $collection = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( 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_items' );
$contributor->add_cap( 'tnc_col_all_edit_published_items' ); $contributor->add_cap( 'tnc_col_all_edit_published_items' );
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/capabilities'); $request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/capabilities');
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() ); $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('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']); $this->assertArrayHasKey('administrator', $caps['tnc_col_' . $collection->get_id() . '_delete_published_items']['roles_inherited']);
} }
function test_get_repo_capabilities() { function test_get_repo_capabilities() {
$role = add_role('test', 'test', ['tnc_rep_edit_metadata'=>true]); $role = add_role('test', 'test', ['tnc_rep_edit_metadata'=>true]);
$request = new \WP_REST_Request('GET', $this->namespace . '/capabilities'); $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('editor', $caps['manage_tainacan']['roles_inherited']);
$this->assertArrayNotHasKey('administrator', $caps['manage_tainacan']['roles_inherited']); $this->assertArrayNotHasKey('administrator', $caps['manage_tainacan']['roles_inherited']);
} }
function test_edit_collection_users_permission() { function test_edit_collection_users_permission() {
global $current_user; global $current_user;
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' )); $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' 'add_cap' => 'tnc_col_12_edit_items'
] ]
); );
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 403, $response->get_status(), 'should not be permitted'); $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; $current_user = $sub_user;
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor'); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
$request->set_query_params( $request->set_query_params(
[ [
'name' => 'Changed name', 'name' => 'Changed name',
'add_cap' => 'tnc_col_12_edit_items' 'add_cap' => 'tnc_col_12_edit_items'
] ]
); );
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 403, $response->get_status(), 'should still not be permitted because edits name'); $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 = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
$request->set_query_params( $request->set_query_params(
[ [
'add_cap' => 'tnc_rep_edit_metadata' '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'); $this->assertEquals( 403, $response->get_status(), 'should not be permitted');
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor'); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
$request->set_query_params( $request->set_query_params(
[ [
'add_cap' => 'tnc_col_12_edit_items' 'add_cap' => 'tnc_col_12_edit_items'
@ -395,13 +361,8 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status(), 'should be permitted'); $this->assertEquals( 200, $response->get_status(), 'should be permitted');
} }
/**
* @group xis
*/
public function test_create_get_roles_with_caps() { public function test_create_get_roles_with_caps() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles'); $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() ); $this->assertEquals( 201, $create->get_status() );
$request = new \WP_REST_Request('GET', $this->namespace . '/roles'); $request = new \WP_REST_Request('GET', $this->namespace . '/roles');
//$request->set_query_params($name_query); //$request->set_query_params($name_query);
$name_response = $this->server->dispatch($request); $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->assertArrayHasKey('tnc_rep_edit_collections', $role['capabilities']);
$this->assertTrue($role['capabilities']['tnc_rep_edit_collections']); $this->assertTrue($role['capabilities']['tnc_rep_edit_collections']);
} }
public function test_edit_role_with_caps() { public function test_edit_role_with_caps() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles'); $request = new \WP_REST_Request('POST', $this->namespace . '/roles');
$request->set_query_params([ $request->set_query_params([
'name' => 'New role', 'name' => 'New role',
'capabilities' => [ 'capabilities' => [
@ -468,8 +425,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
$this->assertArrayHasKey('manage_tainacan_collection_123', $data['capabilities']); $this->assertArrayHasKey('manage_tainacan_collection_123', $data['capabilities']);
$this->assertTrue($data['capabilities']['manage_tainacan_collection_123']); $this->assertTrue($data['capabilities']['manage_tainacan_collection_123']);
// EDIT // EDIT
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); $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() ); $this->assertEquals( 200, $response->get_status() );
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/tainacan-new-role'); $request = new \WP_REST_Request('GET', $this->namespace . '/roles/tainacan-new-role');
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() ); $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('tnc_rep_edit_taxonomies', $data['capabilities']);
$this->assertArrayNotHasKey('manage_tainacan_collection_123', $data['capabilities']); $this->assertArrayNotHasKey('manage_tainacan_collection_123', $data['capabilities']);
} }
function test_new_role_can_read() { function test_new_role_can_read() {
$request = new \WP_REST_Request('POST', $this->namespace . '/roles'); $request = new \WP_REST_Request('POST', $this->namespace . '/roles');
$request->set_query_params(['name' => 'New role']); $request->set_query_params(['name' => 'New role']);
$create = $this->server->dispatch($request); $create = $this->server->dispatch($request);
$this->assertEquals( 201, $create->get_status() ); $this->assertEquals( 201, $create->get_status() );
$role = get_role('tainacan-new-role'); $role = get_role('tainacan-new-role');
$this->assertTrue( $role->has_cap( 'read' ) ); $this->assertTrue( $role->has_cap( 'read' ) );
} }
} }
?>

View File

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

View File

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