new default roles #274

This commit is contained in:
leogermani 2019-11-04 18:55:40 -03:00
parent 59054f32f6
commit 7f4298b3a5
6 changed files with 145 additions and 616 deletions

View File

@ -1,570 +0,0 @@
<?php
namespace Tainacan;
use Tainacan\Repositories\Repository;
class Capabilities {
public $defaults = [
"tainacan-collection"=> [
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"read"
],
"subscriber"=> [
"read"
]
],
"tainacan-metadatum"=> [
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"delete_posts",
"edit_posts",
"read"
],
"subscriber"=> [
"read"
]
],
"tainacan-filter"=> [
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"delete_posts",
"edit_posts",
"read"
],
"subscriber"=> [
"read"
]
],
"tainacan-taxonomy"=> [
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"delete_posts",
"edit_posts",
"read"
],
"subscriber"=> [
"read"
]
],
"tainacan-log"=> [
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"delete_posts",
"edit_posts",
"read"
],
"subscriber"=> [
"read"
]
],
"tainacan-items"=> [ // abstract, will apply to collection Items
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"delete_posts",
"edit_posts",
"read"
],
"subscriber"=> [
"read"
]
],
];
public static $dependencies = [
"tainacan-items" => [
'edit_posts' => 'upload_files',
"edit_private_posts" => 'upload_files',
"edit_published_posts" => 'upload_files',
"edit_others_posts" => 'upload_files'
]
];
private static $instance = null;
public static function get_instance()
{
if(!isset(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
/**
* Register hooks
*/
private function __construct() {
add_action('tainacan-insert-tainacan-collection', array(&$this, 'new_collection'));
add_action('tainacan-add-collection-moderators', array(&$this, 'add_moderators'), 10, 2);
add_action('tainacan-remove-collection-moderators', array(&$this, 'remove_moderators'), 10, 2);
add_filter( 'gettext_with_context', array(&$this, 'translate_user_roles'), 10, 4 );
// Dummy calls for translation.
_x('Tainacan Author', 'User role', 'tainacan');
_x('Tainacan Contributor', 'User role', 'tainacan');
_x('Tainacan Editor', 'User role', 'tainacan');
}
/**
* Tainacan default roles
*
* These are roles relative to the native WordPress roles. They will have
* the same capabilities of their relatives native roles in what is concerned to
* tainacan activities, but will have no other WordPress capabilities.
*
* @return array Tainacan roles
*/
public function get_tainacan_roles() {
$tainacan_roles = [
'editor' => [
'slug' => 'tainacan-editor',
'display_name' => 'Tainacan Editor'
],
'contributor' => [
'slug' => 'tainacan-contributor',
'display_name' => 'Tainacan Contributor'
],
'author' => [
'slug' => 'tainacan-author',
'display_name' => 'Tainacan Author'
],
];
return $tainacan_roles;
}
/**
* Callback to gettext_with_context hook to translate custom ueser roles.
*
* Since user roles are stored in the database, we have to translate them on the fly
* using translate_user_role() function.
*
* @see https://wordpress.stackexchange.com/questions/141551/how-to-auto-translate-custom-user-roles
*/
public function translate_user_roles( $translations, $text, $context, $domain ) {
$plugin_domain = 'tainacan';
$roles_names = array_map(function($role) {
return $role['display_name'];
}, $this->get_tainacan_roles());
if ( $context === 'User role' && in_array( $text, $roles_names ) && $domain !== $plugin_domain ) {
return translate_with_gettext_context( $text, $context, $plugin_domain );
}
return $translations;
}
protected function check_dependencies($role, $post_type, $cap, $add = true) {
if(
array_key_exists($post_type, self::$dependencies) &&
array_key_exists($cap, self::$dependencies[$post_type])
) {
$added = false;
if(! $role->has_cap(self::$dependencies[$post_type][$cap]) && $add) {
$role->add_cap(self::$dependencies[$post_type][$cap]);
$added = true;
}
if($role instanceof \WP_User && $add) { //moderator
$append_caps = get_user_meta($role->ID, '.tainacan-dependecies-caps', true);
if(! is_array($append_caps)) $append_caps = [];
if(
(! array_key_exists(self::$dependencies[$post_type][$cap], $append_caps) && $added ) || // we never added and need to add
(
array_key_exists(self::$dependencies[$post_type][$cap], $append_caps) &&
$append_caps[self::$dependencies[$post_type][$cap]] === false &&
$added
) // we added but before is not need to add
) {
$append_caps[self::$dependencies[$post_type][$cap]] = 0;
}
else { // we to not added this cap
$append_caps[self::$dependencies[$post_type][$cap]] = false;
}
if($append_caps[self::$dependencies[$post_type][$cap]] !== false) {
$append_caps[self::$dependencies[$post_type][$cap]]++; // add 1 to each collection he is a moderator
update_user_meta($role->ID, '.tainacan-dependecies-caps', $append_caps);
}
}
return self::$dependencies[$post_type][$cap];
}
return false;
}
/**
* Update post_type caps using WordPress basic roles and register tainacan roles
*/
public function init() {
$defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults);
foreach ($defaults_caps as $post_type => $wp_append_roles) {
if($post_type == 'tainacan-items') continue;
$entity = Repository::get_entity_by_post_type($post_type);
$entity_cap = $entity->get_capabilities();
// append new capabilities to WordPress default roles
foreach ($wp_append_roles as $role_name => $caps) {
$role = get_role($role_name);
if(!is_object($role)) {
throw new \Exception(sprintf('Role "%s" not found', $role_name));
}
foreach ($caps as $cap) {
$role->add_cap($entity_cap->$cap);
$this->check_dependencies($role, $post_type, $cap);
}
$tainacan_roles = $this->get_tainacan_roles();
if (array_key_exists($role_name, $tainacan_roles)) {
$tainacan_role = add_role( $tainacan_roles[$role_name]['slug'], $tainacan_roles[$role_name]['display_name'] );
if(!is_object($tainacan_role)) {
$tainacan_role = get_role($tainacan_roles[$role_name]['slug']);
}
if(!is_object($tainacan_role)) {
throw new \Exception(sprintf('Role "%s" not found', $tainacan_roles[$role_name]['slug']));
}
foreach ($caps as $cap) {
$tainacan_role->add_cap($entity_cap->$cap);
$this->check_dependencies($tainacan_role, $post_type, $cap);
}
}
}
}
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
foreach ($collections as $collection) {
$this->set_items_capabilities($collection, $defaults_caps);
}
}
/**
* Set config roles for items
* @param \Tainacan\Entities\Collection $collection
*/
public function set_items_capabilities($collection, $defaults_caps = null) {
if(is_null($defaults_caps)) $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); // External Call
$collection_items_caps = $collection->get_items_capabilities();
foreach ($defaults_caps['tainacan-items'] as $role_name => $caps) {
$role = get_role($role_name);
if(!is_object($role)) {
throw new \Exception(sprintf('Role "%s" not found', $role_name));
}
foreach ($caps as $cap) {
$role->add_cap($collection_items_caps->$cap);
$this->check_dependencies($role, 'tainacan-items', $cap);
}
// Tainacan relative role
$role = get_role('tainacan-' . $role_name);
if (\is_object($role)) {
foreach ($caps as $cap) {
$role->add_cap($collection_items_caps->$cap);
$this->check_dependencies($role, 'tainacan-items', $cap);
}
}
}
// Refresh roles capabilities for current user to have instant effect
global $current_user;
$current_user->get_role_caps();
}
/**
* Return list of editable roles
* @return array List of roles
*/
public static function get_editable_roles() {
// TODO $this->security_check();
global $wp_roles;
if(! isset($wp_roles)) $wp_roles = new \WP_Roles();
$all_roles = $wp_roles->get_names();
$editable_roles = apply_filters('tainacan-editable-roles', $all_roles);
return $all_roles;
}
/**
* Hook to set capabilities to the new created collection
* @param \Tainacan\Entities\Collection $collection
*/
public function new_collection($collection)
{
$this->set_items_capabilities($collection);
}
/**
* Hooke to revoke the capabilities for the items post type of the collection
* @param \Tainacan\Entities\Collection $collection The collection object
* @param array $moderators List of IDs of user IDs removed from the moderators list of the collection
* @return void
*/
public function remove_moderators($collection, $moderators) {
$defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults);
if (is_array($moderators)) {
$collection_items_caps = $collection->get_items_capabilities();
foreach ($moderators as $moderator) {
$user = get_userdata($moderator);
if ($user instanceof \WP_User && is_object($collection_items_caps)) {
$caps = $defaults_caps['tainacan-items']['editor'];
foreach ($caps as $cap) {
$user->remove_cap($collection_items_caps->$cap);
$dep_cap = $this->check_dependencies($user, 'tainacan-items', $cap, false);
if($dep_cap !== false) {
$appended_caps = get_user_meta($user->ID, '.tainacan-dependecies-caps', true);
if(array_key_exists($dep_cap, $appended_caps) && $appended_caps[$dep_cap] !== false && $appended_caps[$dep_cap] > 0) {
$appended_caps[$dep_cap]--;
update_user_meta($user->ID, '.tainacan-dependecies-caps', $appended_caps);
if($appended_caps == 0) { // they are not a moderator of a collection, remove cap at all
$user->remove_cap($cap);
}
}
}
}
}
}
}
}
/**
* Hooke to grant the capabilities for the items post type of the collection
* @param \Tainacan\Entities\Collection $collection The collection object
* @param array $moderators List of IDs of user IDs added to the moderators list of the collection
* @return void
*/
public function add_moderators($collection, $moderators) {
$defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults);
if (is_array($moderators)) {
$collection_items_caps = $collection->get_items_capabilities();
foreach ($moderators as $moderator) {
$user = get_userdata($moderator);
if ($user instanceof \WP_User && is_object($collection_items_caps)) {
$caps = $defaults_caps['tainacan-items']['editor'];
foreach ($caps as $cap) {
$user->add_cap($collection_items_caps->$cap);
$this->check_dependencies($user, 'tainacan-items', $cap);
}
}
}
}
}
}

View File

@ -195,6 +195,89 @@ class Roles {
add_filter( 'user_has_cap', [$this, 'user_has_cap_filter'], 10, 4 ); add_filter( 'user_has_cap', [$this, 'user_has_cap_filter'], 10, 4 );
add_filter( 'map_meta_cap', [$this, 'map_meta_cap'], 10, 4 ); add_filter( 'map_meta_cap', [$this, 'map_meta_cap'], 10, 4 );
add_filter( 'gettext_with_context', array(&$this, 'translate_user_roles'), 10, 4 );
// Dummy calls for translation.
_x('Tainacan Administrator', 'User role', 'tainacan');
_x('Tainacan Editor', 'User role', 'tainacan');
_x('Tainacan Author', 'User role', 'tainacan');
}
/**
* Tainacan default roles
*
* @return array Tainacan roles
*/
public function get_tainacan_roles() {
$tainacan_roles = [
'administrator' => [
'slug' => 'tainacan-administrator',
'display_name' => 'Tainacan Administrator',
'caps' => [
'manage_tainacan' => true
]
],
'editor' => [
'slug' => 'tainacan-editor',
'display_name' => 'Tainacan Editor',
'caps' => [
'tnc_rep_edit_collections' => true,
'tnc_rep_delete_collections' => true,
'tnc_rep_edit_taxonomies' => true,
'tnc_rep_edit_others_taxonomies' => true,
'tnc_rep_delete_taxonomies' => true,
'tnc_rep_delete_others_taxonomies' => true,
'tnc_rep_edit_metadata' => true,
'tnc_rep_edit_filters' => true,
'tnc_rep_delete_metadata' => true,
'tnc_rep_delete_filters' => true,
'tnc_rep_read_private_collections' => true,
'tnc_rep_read_private_taxonomies' => true,
'tnc_rep_read_private_metadata' => true,
'tnc_rep_read_private_filters' => true,
'tnc_rep_read_logs' => true,
'manage_tainacan_collection_all' => true
]
],
'author' => [
'slug' => 'tainacan-author',
'display_name' => 'Tainacan Author',
'caps' => [
'tnc_rep_edit_collections' => true,
'tnc_rep_edit_taxonomies' => true,
'tnc_rep_read_private_collections' => true,
'tnc_rep_read_private_taxonomies' => true,
'tnc_rep_read_private_metadata' => true,
'tnc_rep_read_private_filters' => true,
]
],
];
return $tainacan_roles;
}
/**
* Callback to gettext_with_context hook to translate custom ueser roles.
*
* Since user roles are stored in the database, we have to translate them on the fly
* using translate_user_role() function.
*
* @see https://wordpress.stackexchange.com/questions/141551/how-to-auto-translate-custom-user-roles
*/
public function translate_user_roles( $translations, $text, $context, $domain ) {
$plugin_domain = 'tainacan';
$roles_names = array_map(function($role) {
return $role['display_name'];
}, $this->get_tainacan_roles());
if ( $context === 'User role' && in_array( $text, $roles_names ) && $domain !== $plugin_domain ) {
return translate_with_gettext_context( $text, $context, $plugin_domain );
}
return $translations;
} }
public function get_all_caps() { public function get_all_caps() {
@ -205,6 +288,21 @@ class Roles {
return array_keys($this->capabilities); return array_keys($this->capabilities);
} }
public function init_default_roles() {
foreach ($this->get_tainacan_roles() as $role) {
$new_role = add_role(
$role['slug'],
$role['display_name'],
$role['caps']
);
}
$admin = get_role('administrator');
$admin->add_cap('manage_tainacan');
}
/** /**
* Gets the capabilty generic name as present in * Gets the capabilty generic name as present in
* Tainacan\Roles::capabilities * Tainacan\Roles::capabilities
@ -256,7 +354,7 @@ class Roles {
// check if the user is the owner // check if the user is the owner
$collection = tainacan_collections()->fetch( (int) $col_id ); $collection = tainacan_collections()->fetch( (int) $col_id );
if ( $collection instanceof \Tainacan\Entities\Collection ) { if ( $collection instanceof \Tainacan\Entities\Collection ) {
if ( $collection->get_author_id() == $user->ID ) { if ( (int) $collection->get_author_id() == (int) $user->ID ) {
$allcaps = array_merge($allcaps, [ $cap => true ]); $allcaps = array_merge($allcaps, [ $cap => true ]);
} }
} }

View File

@ -180,9 +180,9 @@ class Collection extends Entity {
return (object) [ return (object) [
// meta // meta
'edit_post' => "tnc_col_edit_item", 'edit_post' => "tnc_rep_edit_collection",
'read_post' => "tnc_col_read_item", 'read_post' => "tnc_rep_read_collection",
'delete_post' => "tnc_col_delete_item", 'delete_post' => "tnc_rep_delete_collection",
// primitive // primitive
'edit_posts' => "tnc_rep_edit_collections", 'edit_posts' => "tnc_rep_edit_collections",

View File

@ -177,8 +177,6 @@ $Tainacan_Elastic_press = \Tainacan\Elastic_Press::get_instance();
require_once(__DIR__ . '/class-tainacan-background-process-heartbeat.php'); require_once(__DIR__ . '/class-tainacan-background-process-heartbeat.php');
$Tainacan_Importer_Heartbeat = new \Tainacan\Background_Importer_Heartbeat(); $Tainacan_Importer_Heartbeat = new \Tainacan\Background_Importer_Heartbeat();
$Tainacan_Capabilities = \Tainacan\Capabilities::get_instance();
$Tainacan_Roles = \Tainacan\Roles::get_instance(); $Tainacan_Roles = \Tainacan\Roles::get_instance();
$TainacanPrivateFiles = \Tainacan\Private_Files::get_instance(); $TainacanPrivateFiles = \Tainacan\Private_Files::get_instance();

View File

@ -105,11 +105,6 @@ class Migrations {
} }
static function init_capabilites() {
$Tainacan_Capabilities = \Tainacan\Capabilities::get_instance();
$Tainacan_Capabilities->init();
}
static function tainacan_migrate_post_type_field_to_metadatum(){ static function tainacan_migrate_post_type_field_to_metadatum(){
global $wpdb; global $wpdb;
@ -419,6 +414,10 @@ class Migrations {
} }
static function init_default_roles() {
\tainacan_roles()->init_default_roles();
}
} }

View File

@ -17,6 +17,7 @@ class Collections extends TAINACAN_UnitTestCase {
* @group permissions * @group permissions
*/ */
function test_permissions () { function test_permissions () {
global $current_user;
$collection_test = $this->tainacan_entity_factory->create_entity( $collection_test = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -30,7 +31,7 @@ class Collections extends TAINACAN_UnitTestCase {
wp_set_current_user($new_user); wp_set_current_user($new_user);
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$this->assertEquals($new_user, $user_id); $this->assertEquals($new_user, $user_id);
$autor1 = $this->factory()->user->create(array( 'role' => 'author' )); $autor1 = $this->factory()->user->create(array( 'role' => 'author' ));
wp_set_current_user($autor1); wp_set_current_user($autor1);
$autor1_id = get_current_user_id(); $autor1_id = get_current_user_id();
@ -55,28 +56,31 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$autor_2_user = get_userdata( $autor2 );
$autor_2_user->add_cap('tnc_rep_edit_collections');
$current_user = $autor_2_user;
$current_user_id = get_current_user_id(); $current_user_id = get_current_user_id();
$this->assertEquals($autor2, $current_user_id); $this->assertEquals($autor2, $current_user_id);
$this->assertFalse(current_user_can($collection_test->cap->edit_post, $collection_test->WP_Post->ID)); $this->assertFalse(current_user_can($collection_test->cap->edit_post, $collection_test->WP_Post->ID));
$this->assertTrue(current_user_can($collection_test2->cap->edit_post, $collection_test2->WP_Post->ID)); $this->assertTrue(current_user_can($collection_test2->cap->edit_post, $collection_test2->WP_Post->ID));
$this->assertFalse(user_can($autor2, $collection_test->cap->edit_post, $collection_test->WP_Post->ID)); $this->assertFalse(user_can($autor2, $collection_test->cap->edit_post, $collection_test->WP_Post->ID));
} }
function debug_meta($user = false) function debug_meta($user = false)
{ {
if($user !== false) wp_set_current_user($user); if($user !== false) wp_set_current_user($user);
$data = get_userdata( get_current_user_id() ); $data = get_userdata( get_current_user_id() );
if ( is_object( $data) ) { if ( is_object( $data) ) {
$current_user_caps = $data->allcaps; $current_user_caps = $data->allcaps;
// print it to the screen // print it to the screen
echo '<pre>' .print_r($data->ID, true).'\n'.print_r($data->display_name, true).'\n'. print_r( $current_user_caps, true ) . '</pre>'; echo '<pre>' .print_r($data->ID, true).'\n'.print_r($data->display_name, true).'\n'. print_r( $current_user_caps, true ) . '</pre>';
} }
} }
/** /**
* A single example test. * A single example test.
*/ */
@ -92,9 +96,9 @@ class Collections extends TAINACAN_UnitTestCase {
); );
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$this->assertEquals('Tainacan\Entities\Collection', get_class($x)); $this->assertEquals('Tainacan\Entities\Collection', get_class($x));
$test = $Tainacan_Collections->fetch($x->get_id()); $test = $Tainacan_Collections->fetch($x->get_id());
$this->assertEquals('teste', $test->get_name()); $this->assertEquals('teste', $test->get_name());
@ -102,7 +106,7 @@ class Collections extends TAINACAN_UnitTestCase {
$this->assertEquals('DESC', $test->get_default_order()); $this->assertEquals('DESC', $test->get_default_order());
$this->assertEquals('draft', $test->get_status()); $this->assertEquals('draft', $test->get_status());
} }
function test_unique_slugs() { function test_unique_slugs() {
$x = $this->tainacan_entity_factory->create_entity( $x = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
@ -115,7 +119,7 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$y = $this->tainacan_entity_factory->create_entity( $y = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -127,9 +131,9 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$this->assertNotEquals($x->get_slug(), $y->get_slug()); $this->assertNotEquals($x->get_slug(), $y->get_slug());
// Create as draft and publish later // Create as draft and publish later
$x = $this->tainacan_entity_factory->create_entity( $x = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
@ -141,7 +145,7 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$y = $this->tainacan_entity_factory->create_entity( $y = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -152,9 +156,9 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$this->assertEquals($x->get_slug(), $y->get_slug()); $this->assertEquals($x->get_slug(), $y->get_slug());
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$x->set_status('publish'); $x->set_status('publish');
$x->validate(); $x->validate();
@ -162,11 +166,11 @@ class Collections extends TAINACAN_UnitTestCase {
$y->set_status('private'); // or publish shoud behave the same $y->set_status('private'); // or publish shoud behave the same
$y->validate(); $y->validate();
$y = $Tainacan_Collections->insert($y); $y = $Tainacan_Collections->insert($y);
$this->assertNotEquals($x->get_slug(), $y->get_slug()); $this->assertNotEquals($x->get_slug(), $y->get_slug());
} }
function test_item() { function test_item() {
$x = $this->tainacan_entity_factory->create_entity( $x = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
@ -194,13 +198,13 @@ class Collections extends TAINACAN_UnitTestCase {
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$item = $Tainacan_Items->fetch( $i->get_id() ); $item = $Tainacan_Items->fetch( $i->get_id() );
$this->assertEquals($item->get_title(), 'item test'); $this->assertEquals($item->get_title(), 'item test');
$this->assertEquals($item->get_description(), 'adasdasdsa'); $this->assertEquals($item->get_description(), 'adasdasdsa');
$this->assertEquals($item->get_collection_id(), $collection->get_id()); $this->assertEquals($item->get_collection_id(), $collection->get_id());
} }
function test_validation() { function test_validation() {
$x = $this->tainacan_entity_factory->create_entity( $x = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
@ -214,25 +218,25 @@ class Collections extends TAINACAN_UnitTestCase {
$this->assertFalse($x->validate()); $this->assertFalse($x->validate());
$this->assertTrue(sizeof($x->get_errors()) > 0); $this->assertTrue(sizeof($x->get_errors()) > 0);
$x->set_default_order('ASDASD'); $x->set_default_order('ASDASD');
$this->assertFalse($x->validate()); $this->assertFalse($x->validate());
$this->assertTrue(sizeof($x->get_errors()) > 0); $this->assertTrue(sizeof($x->get_errors()) > 0);
$x->set_default_order('DESC'); $x->set_default_order('DESC');
$this->assertTrue($x->validate()); $this->assertTrue($x->validate());
$this->assertTrue(empty($x->get_errors())); $this->assertTrue(empty($x->get_errors()));
} }
function test_hooks() { function test_hooks() {
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!'); $this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!');
} }
function test_create_child_collection() { function test_create_child_collection() {
$x = $this->tainacan_entity_factory->create_entity( $x = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -242,7 +246,7 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$col = $this->tainacan_entity_factory->create_entity( $col = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -253,17 +257,17 @@ class Collections extends TAINACAN_UnitTestCase {
), ),
true true
); );
$Collections = \Tainacan\Repositories\Collections::get_instance(); $Collections = \Tainacan\Repositories\Collections::get_instance();
$col->set_parent($x->get_id()); $col->set_parent($x->get_id());
$col->set_status('publish'); $col->set_status('publish');
$col->validate(); $col->validate();
$col = $Collections->insert($col); $col = $Collections->insert($col);
$this->assertEquals($x->get_id(), $col->get_parent()); $this->assertEquals($x->get_id(), $col->get_parent());
} }
} }