Logs and Tests

Updated action do insert logs
Fixes action of insert logs in all insert methods
Updated message of logs
Updated description of logs
Fixes some tests
This commit is contained in:
weryques 2018-04-09 12:45:49 -03:00
parent 73bf460ab3
commit dba92385a6
8 changed files with 68 additions and 12 deletions

View File

@ -113,7 +113,7 @@
for (let event of eventsList)
event['by'] = this.$i18n.get('info_by') +
event['user_name'] + '<br>' + this.$i18n.get('info_date') +
moment(event['log_date'], 'YYYY-MM-DD').format('DD/MM/YYYY');
moment(event['log_date'], 'YYYY-MM-DD h:mm:ss').format('DD/MM/YYYY, hh:mm:ss');
return eventsList;
}

View File

@ -28,6 +28,14 @@ class Item_Metadata extends Repository {
public function insert($item_metadata) {
$old = $item_metadata;
$is_update = false;
// TODO get props obj before update
if( $item_metadata->get_id() ) {
$is_update = true;
$old = $item_metadata->get_repository()->fetch( $item_metadata->get_id() );
}
$unique = !$item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object();
@ -75,7 +83,7 @@ class Item_Metadata extends Repository {
do_action('tainacan-insert', $item_metadata);
do_action('tainacan-insert', $item_metadata, $old, $is_update);
do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata);
$new_entity = new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field());

View File

@ -152,6 +152,14 @@ class Items extends Repository {
public function insert( $item ) {
$old = $item;
$is_update = false;
// TODO get props obj before update
if( $item->get_id() ) {
$is_update = true;
$old = $item->get_repository()->fetch( $item->get_id() );
}
$map = $this->get_map();
// get collection to determine post type
@ -198,7 +206,7 @@ class Items extends Repository {
set_post_thumbnail( $item->WP_Post, $item->get_featured_img_id( $item->WP_Post->ID ) );
}
do_action( 'tainacan-insert', $item );
do_action( 'tainacan-insert', $item, $old, $is_update );
do_action( 'tainacan-insert-Item', $item );
// return a brand new object

View File

@ -28,7 +28,7 @@ class Logs extends Repository {
protected function __construct() {
parent::__construct();
add_action( 'tainacan-insert', array( $this, 'log_inserts' ), 10, 2 );
add_action( 'tainacan-insert', array( $this, 'log_inserts' ), 10, 3 );
}
public function get_map() {
@ -211,6 +211,7 @@ class Logs extends Repository {
$args = [
'post_type' => Entities\Log::get_post_type(),
'posts_per_page' => -1,
'orderby' => 'date',
];
$logs = $this->fetch( $args, 'OBJECT' );
@ -226,7 +227,7 @@ class Logs extends Repository {
*
* @return Entities\Log new created log
*/
public function log_inserts( $new_value, $old_value = null ) {
public function log_inserts( $new_value, $old_value = null, $is_update = null ) {
$msn = "";
$description = "";
@ -242,8 +243,29 @@ class Logs extends Repository {
$name = method_exists($new_value, 'get_name') ? $new_value->get_name() :
(method_exists($new_value, 'get_title') ? $new_value->get_title() : $new_value->get_field()->get_name());
$msn = sprintf( esc_html__( 'A %s has been created/updated.', 'tainacan' ), $class_name);
$description = sprintf( esc_html__("The %s %s has been created/updated.", 'tainacan' ), $name, strtolower($class_name));
$articleA = 'A';
$articleAn = 'An';
$vowels = 'aeiou';
if($is_update){
if(substr_count($vowels, strtolower(substr($class_name, 0, 1))) > 0){
$msn = sprintf( __( '%s %s has been updated.', 'tainacan' ), $articleAn, $class_name);
$description = sprintf( __("The \"%s\" %s has been updated.", 'tainacan' ), $name, strtolower($class_name));
} else {
$msn = sprintf( __( '%s %s has been updated.', 'tainacan' ), $articleA, $class_name);
$description = sprintf( __("The \"%s\" %s has been updated.", 'tainacan' ), $name, strtolower($class_name));
}
} else {
if(substr_count($vowels, strtolower(substr($class_name, 0, 1))) > 0){
$msn = sprintf( __( '%s %s has been created.', 'tainacan' ), $articleAn, $class_name);
$description = sprintf( __("The \"%s\" %s has been created.", 'tainacan' ), $name, strtolower($class_name));
} else {
$msn = sprintf( __( '%s %s has been created.', 'tainacan' ), $articleA, $class_name);
$description = sprintf( __("The \"%s\" %s has been created.", 'tainacan' ), $name, strtolower($class_name));
}
}
}

View File

@ -77,8 +77,10 @@ abstract class Repository {
}
$old = $obj;
$is_update = false;
// TODO get props obj before update
if( $obj->get_id() ) {
$is_update = true;
$old = $obj->get_repository()->fetch( $obj->get_id() );
}
@ -116,7 +118,7 @@ abstract class Repository {
set_post_thumbnail( $obj->WP_Post, $obj->get_featured_img_id( $obj->WP_Post->ID ) );
}
do_action( 'tainacan-insert', $obj, $old );
do_action( 'tainacan-insert', $obj, $old, $is_update );
do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj );
// return a brand new object
@ -609,6 +611,7 @@ abstract class Repository {
*/
public function diff( $old = 0, $new ) {
$old_entity = null;
if ( $old === 0 ) { // self diff or other entity?
$id = $new->get_id();
if ( ! empty( $id ) ) { // there is a repository entity?
@ -620,6 +623,7 @@ abstract class Repository {
} else { // get entity from repository
$old_entity = $this->get_entity_by_post( $old );
}
$new_entity = $this->get_entity_by_post( $new );
$map = $this->get_map();
@ -628,8 +632,10 @@ abstract class Repository {
foreach ( $map as $prop => $mapped ) {
if ( $old_entity->get_mapped_property( $prop ) != $new_entity->get_mapped_property( $prop ) ) {
if ( $mapped['map'] == 'meta_multi' ) {
$meta_diff = array_diff( $new_entity->get_mapped_property( $prop ), $old_entity->get_mapped_property( $prop ) );
if ( ! empty( $meta_diff ) ) {
$diff[ $prop ] = [
'new' => $new_entity->get_mapped_property( $prop ),
@ -643,8 +649,10 @@ abstract class Repository {
'old' => $old_entity->get_mapped_property( $prop )
];
}
}
}
$diff = apply_filters( 'tainacan-entity-diff', $diff, $new, $old );
return $diff;

View File

@ -100,6 +100,15 @@ class Terms extends Repository {
* @return Entities\Entity|Entities\Term
*/
public function insert($term){
$old = $term;
$is_update = false;
// TODO get props obj before update
if( $term->get_id() ) {
$is_update = true;
$old = $term->get_repository()->fetch( $term->get_id() );
}
// First iterate through the native post properties
$map = $this->get_map();
foreach ($map as $prop => $mapped) {
@ -138,7 +147,7 @@ class Terms extends Repository {
}
}
do_action('tainacan-insert', $term);
do_action('tainacan-insert', $term, $old, $is_update);
do_action('tainacan-insert-Term', $term);
return new Entities\Term($term_saved['term_id'], $term->get_taxonomy());

View File

@ -238,8 +238,10 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
$first_filter = $data[0];
$second_filter = $data[1];
$this->assertEquals($filter->get_name(), $first_filter['name']);
$this->assertEquals($filter2->get_name(), $second_filter['name']);
$names = [$first_filter['name'], $second_filter['name']];
$this->assertContains($filter->get_name(), $names);
$this->assertContains($filter2->get_name(), $names);
#### FETCH A FILTER ####

View File

@ -99,7 +99,6 @@ class Logs extends TAINACAN_UnitTestCase {
$log = $Tainacan_Logs->fetch_last();
$diff = $log->diff();
$this->assertEquals('With name', $diff['name']['new']);