fix tests, pagination on query and pos on delete #200

This commit is contained in:
Vinícius Nunes 2019-12-15 22:05:30 -03:00
parent bccc745806
commit 39c89d343d
3 changed files with 33 additions and 19 deletions

View File

@ -114,12 +114,22 @@ class Bulk_Edit_Process extends Generic_Process {
$itemsRepo = \Tainacan\Repositories\Items::get_instance(); $itemsRepo = \Tainacan\Repositories\Items::get_instance();
$count = $this->get_in_step_count(); $count = $this->get_in_step_count();
$params['query']['fields'] = 'ids'; $post_per_page = 1;
$params['query']['posts_per_page'] = 1; if ( isset($params['query']['posts_per_page']) && $params['query']['posts_per_page'] != -1 ) {
$params['query']['offset'] = $count++; $post_per_page = $params['query']['posts_per_page'] - $count;
$params['query']['nopaging'] = false; $params['query']['posts_per_page'] = $post_per_page;
}
if($post_per_page <= 0) {
return false;
}
$item_query = $itemsRepo->fetch($params['query'], $params['collection_id']); $query = $params['query'];
$query['fields'] = 'ids';
$query['posts_per_page'] = $post_per_page;
$query['offset'] = $count++;
$query['nopaging'] = false;
$item_query = $itemsRepo->fetch($query, $params['collection_id']);
if(!$item_query->have_posts() ) { if(!$item_query->have_posts() ) {
$params['control_metadata'] = true; $params['control_metadata'] = true;
$this->save_options($params); $this->save_options($params);
@ -204,7 +214,10 @@ class Bulk_Edit_Process extends Generic_Process {
} }
$this->add_log( sprintf( __('bulk edit has process the item ID: "%d"', 'tainacan'), $item->get_id() ) ); $this->add_log( sprintf( __('bulk edit has process the item ID: "%d"', 'tainacan'), $item->get_id() ) );
$this->$method($item); $add_steps = $this->$method($item);
if ( is_int($add_steps) ) {
$count = $count + $add_steps;
}
return $count; return $count;
} }
@ -220,7 +233,8 @@ class Bulk_Edit_Process extends Generic_Process {
$this->add_error_log( sprintf( __( 'Please verify, invalid value(s) to edit item ID: "%d"', 'tainacan' ), $item->get_id() ) ); $this->add_error_log( sprintf( __( 'Please verify, invalid value(s) to edit item ID: "%d"', 'tainacan' ), $item->get_id() ) );
$serealize_erro = (object) array('err' => array()); $serealize_erro = (object) array('err' => array());
array_walk_recursive($item_metadata->get_errors(), create_function('&$v, $k, &$t', '$t->err[] = $v;'), $serealize_erro); $erro = $item_metadata->get_errors();
array_walk_recursive($erro, function($v, $k, &$t) {$t->err[] = $v;}, $serealize_erro);
$this->add_error_log( __('errors: ', 'tainacan') . implode(", ", $serealize_erro->err) ); $this->add_error_log( __('errors: ', 'tainacan') . implode(", ", $serealize_erro->err) );
return false; return false;
@ -235,9 +249,8 @@ class Bulk_Edit_Process extends Generic_Process {
$item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum ); $item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum );
if($item_metadata->is_multiple()) { if($item_metadata->is_multiple()) {
$value = is_array( $value ) ? $value : [$value];
$item_metadata->set_value( $value ); $item_metadata->set_value( $value );
} elseif(is_array($value)) {
$item_metadata->set_value(implode(' ', $value));
} else { } else {
$item_metadata->set_value($value); $item_metadata->set_value($value);
} }
@ -327,9 +340,9 @@ class Bulk_Edit_Process extends Generic_Process {
if($metadatum->get_id() == $metadatum_id) { if($metadatum->get_id() == $metadatum_id) {
$values = is_array($item_metadata->get_value()) ? $item_metadata->get_value() : [$item_metadata->get_value()]; $values = is_array($item_metadata->get_value()) ? $item_metadata->get_value() : [$item_metadata->get_value()];
$pos = array_search($old_value, $values); $pos = array_search($old_value, $values);
if ($pos != false) { if ($pos !== false) {
$values[$pos] = $new_value; $values[$pos] = $new_value;
$item_metadata->set_value( $values ); $item_metadata->set_value( $metadatum->is_multiple() ? $values : $values[$pos] );
return $this->save_item_metadata($item_metadata, $item); return $this->save_item_metadata($item_metadata, $item);
} }
return false; return false;
@ -355,11 +368,16 @@ class Bulk_Edit_Process extends Generic_Process {
} }
private function delete_items(\Tainacan\Entities\Item $item) { private function delete_items(\Tainacan\Entities\Item $item) {
if ('trash' != $item->get_status() ) {
$this->add_error_log( sprintf( __('Items must be on trash to be deleted, item ID: "%d"', 'tainacan'), $item->get_id() ) );
return false;
}
if ( !$this->items_repository->delete($item) ) { if ( !$this->items_repository->delete($item) ) {
$this->add_error_log( sprintf( __('error on send to trash, item ID: "%d"', 'tainacan'), $item->get_id() ) ); $this->add_error_log( sprintf( __('error on send to trash, item ID: "%d"', 'tainacan'), $item->get_id() ) );
return false; return false;
} }
return true; return -1;
} }
private function set_status(\Tainacan\Entities\Item $item) { private function set_status(\Tainacan\Entities\Item $item) {

View File

@ -331,7 +331,7 @@ abstract class Generic_Process {
//Move on to the next step //Move on to the next step
$this->set_in_step_count(0); $this->set_in_step_count(0);
$return = $this->next_step(); $return = $this->next_step();
} else if(is_numeric($result) && $result > 0) { } else if(is_numeric($result) && $result >= 0) {
$this->set_in_step_count($result); $this->set_in_step_count($result);
$return = $result; $return = $result;
} }

View File

@ -1088,7 +1088,8 @@ class BulkEditBgProcess extends TAINACAN_UnitApiTestCase {
global $wpdb; global $wpdb;
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_content = 'test_description'" ); $post_type = $this->collection->get_db_identifier();
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_content = 'test_description' and post_type = '$post_type'" );
$this->assertEquals(7, $count); $this->assertEquals(7, $count);
@ -1561,11 +1562,6 @@ class BulkEditBgProcess extends TAINACAN_UnitApiTestCase {
'posts_per_page' => 5 'posts_per_page' => 5
]; ];
$bulk = new \Tainacan\Bulk_Edit([
'query' => $query,
'collection_id' => $this->collection->get_id()
]);
$process = $this->new_process( $process = $this->new_process(
[ [
'query' => $query, 'query' => $query,