move initial capabilities from init hook to activation hook

This commit is contained in:
Leo Germani 2018-03-11 19:53:15 -03:00
parent b0f7c3b1ad
commit 66481113d4
5 changed files with 53 additions and 6 deletions

View File

@ -4,7 +4,7 @@ namespace Tainacan;
use Tainacan\Repositories\Repository;
class Capabilities {
protected $defaults = [
public $defaults = [
"tainacan-collection"=> [
"administrator"=> [
"delete_posts",
@ -275,7 +275,6 @@ class Capabilities {
* Register hooks
*/
function __construct() {
add_action('init', array(&$this, 'init'), 11);
add_action('tainacan-insert-tainacan-collection', array(&$this, 'new_collection'));
add_action('tainacan-add-collection-moderators', array(&$this, 'add_moderators'), 10, 2);

View File

@ -1,7 +1,5 @@
<?php
use Tainacan\Capabilities;
const ENTITIES_DIR = __DIR__ . '/entities/';
const FIELD_TYPES_DIR = __DIR__ . '/field-types/';
const FILTER_TYPES_DIR = __DIR__ . '/filter-types/';
@ -109,6 +107,4 @@ $Tainacan_Terms = new \Tainacan\Repositories\Terms();
global $Tainacan_Logs;
$Tainacan_Logs = new \Tainacan\Repositories\Logs();
new Capabilities();
?>

View File

@ -25,6 +25,10 @@ function tnc_enable_dev_wp_interface() {
//return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
}
global $Tainacan_Capabilities;
$Tainacan_Capabilities = new \Tainacan\Capabilities();
register_activation_hook( __FILE__, array( $Tainacan_Capabilities, 'init' ) );
// TODO move it somewhere else?
require_once('admin/class-tainacan-admin.php');
global $Tainacan_Admin;

View File

@ -22,6 +22,9 @@ require_once $_tests_dir . '/includes/functions.php';
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/src/tainacan.php';
add_action('plugins_loaded', function() {
do_action('activate_' . substr(dirname( dirname( __FILE__ ) ), 1) . '/src/tainacan.php');
});
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

View File

@ -0,0 +1,45 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Repositories\Repository;
/**
* Class DefaultCapabilities
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class DefaultCapabilities extends TAINACAN_UnitTestCase {
function test_capabilities_present() {
global $Tainacan_Capabilities;
$c = new \Tainacan\Entities\Collection();
$caps = $c->get_capabilities();
$defaults_caps = $Tainacan_Capabilities->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();
foreach ($wp_append_roles as $role_name => $caps) {
$role = get_role($role_name);
$new_user = $this->factory()->user->create(array( 'role' => $role_name ));
wp_set_current_user($new_user);
foreach ($caps as $cap) {
$this->assertTrue(current_user_can($entity_cap->$cap));
}
}
}
}
}