2017-10-20 14:30:04 +00:00
|
|
|
<?php
|
2017-11-15 18:50:11 +00:00
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
2017-10-20 14:30:04 +00:00
|
|
|
/**
|
|
|
|
* Class TestCollections
|
|
|
|
*
|
|
|
|
* @package Test_Tainacan
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sample test case.
|
|
|
|
*/
|
2017-12-03 20:33:16 +00:00
|
|
|
class Collections extends TAINACAN_UnitTestCase {
|
2017-10-20 14:30:04 +00:00
|
|
|
|
2017-12-21 00:13:33 +00:00
|
|
|
/**
|
|
|
|
* @group permissions
|
|
|
|
*/
|
|
|
|
function test_permissions () {
|
2018-01-18 14:33:56 +00:00
|
|
|
$collection_test = $this->tainacan_entity_factory->create_entity(
|
2017-12-26 22:08:15 +00:00
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testeCaps',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 'DESC'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-12-21 00:13:33 +00:00
|
|
|
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
|
|
|
wp_set_current_user($new_user);
|
|
|
|
$user_id = get_current_user_id();
|
|
|
|
$this->assertEquals($new_user, $user_id);
|
2017-12-26 22:08:15 +00:00
|
|
|
|
|
|
|
$autor1 = $this->factory()->user->create(array( 'role' => 'author' ));
|
|
|
|
wp_set_current_user($autor1);
|
|
|
|
$autor1_id = get_current_user_id();
|
2018-01-18 14:33:56 +00:00
|
|
|
$collection_test = $this->tainacan_entity_factory->create_entity(
|
2017-12-26 22:08:15 +00:00
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testeCapsOwner',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 'DESC'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-01-18 14:33:56 +00:00
|
|
|
$this->assertEquals($autor1_id, $collection_test->WP_Post->post_author);
|
2018-01-24 19:32:06 +00:00
|
|
|
$autor2 = $this->factory()->user->create(array( 'role' => 'author', 'display_name' => 'Author2' ));
|
2017-12-26 22:08:15 +00:00
|
|
|
wp_set_current_user($autor2);
|
2018-01-18 14:33:56 +00:00
|
|
|
$collection_test2 = $this->tainacan_entity_factory->create_entity(
|
2018-01-10 23:09:41 +00:00
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testeCapsOwner2',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 'DESC'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-12-26 22:08:15 +00:00
|
|
|
$current_user_id = get_current_user_id();
|
|
|
|
$this->assertEquals($autor2, $current_user_id);
|
2018-01-18 14:33:56 +00:00
|
|
|
$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->assertFalse(user_can($autor2, $collection_test->cap->edit_post, $collection_test->WP_Post->ID));
|
|
|
|
|
2018-01-29 01:34:12 +00:00
|
|
|
// add current user to moderators list of collection test.
|
|
|
|
// Test add_moderator method and granting permissions
|
|
|
|
|
|
|
|
$collection_test->add_moderator_id($current_user_id);
|
|
|
|
$collection_test->validate();
|
|
|
|
global $Tainacan_Collections;
|
|
|
|
|
|
|
|
$collection_test = $Tainacan_Collections->insert($collection_test);
|
|
|
|
|
|
|
|
$this->assertContains($current_user_id, $collection_test->get_moderators_ids());
|
|
|
|
$this->assertTrue(current_user_can($collection_test->cap->edit_post, $collection_test->WP_Post->ID));
|
|
|
|
|
2018-01-18 14:33:56 +00:00
|
|
|
wp_set_current_user($this->user_id);
|
|
|
|
$collection_test_moderator = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testeModerator',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 'DESC',
|
|
|
|
'moderators_ids' => [$autor2]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertEquals([$autor2], $collection_test_moderator->get_moderators_ids());
|
2018-01-27 02:23:25 +00:00
|
|
|
|
|
|
|
wp_set_current_user($autor2);
|
|
|
|
$this->assertTrue(current_user_can($collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
2018-01-24 19:32:06 +00:00
|
|
|
$this->assertTrue($collection_test_moderator->can_edit($autor2), 'Moderators cannot edit a collection!');
|
2018-01-29 01:34:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
// now lets test adding a moderator in a collection that already has one
|
|
|
|
// and then lets test remove_moderator_id method
|
|
|
|
|
|
|
|
// first, subscriber user should not be able to edit the collection
|
|
|
|
$this->assertFalse(user_can($new_user, $collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
|
|
|
|
|
|
|
// lets add him as moderator
|
|
|
|
$collection_test_moderator->add_moderator_id($new_user);
|
|
|
|
$collection_test_moderator->validate();
|
|
|
|
$collection_test_moderator = $Tainacan_Collections->insert($collection_test_moderator);
|
|
|
|
$this->assertContains($new_user, $collection_test_moderator->get_moderators_ids());
|
|
|
|
|
|
|
|
|
|
|
|
// now he can edit
|
|
|
|
$this->assertTrue(user_can($new_user, $collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
|
|
|
|
|
|
|
// lets remove him and check if he can no longer edit
|
|
|
|
$collection_test_moderator->remove_moderator_id($new_user);
|
|
|
|
$collection_test_moderator->validate();
|
|
|
|
$collection_test_moderator = $Tainacan_Collections->insert($collection_test_moderator);
|
|
|
|
$this->assertNotContains($new_user, $collection_test_moderator->get_moderators_ids());
|
|
|
|
|
|
|
|
|
|
|
|
// now he can edit
|
|
|
|
$this->assertFalse(user_can($new_user, $collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
|
|
|
|
2018-01-24 19:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function debug_meta($user = false)
|
|
|
|
{
|
|
|
|
if($user !== false) wp_set_current_user($user);
|
|
|
|
$data = get_userdata( get_current_user_id() );
|
|
|
|
|
|
|
|
if ( is_object( $data) ) {
|
|
|
|
$current_user_caps = $data->allcaps;
|
|
|
|
|
|
|
|
// 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>';
|
|
|
|
}
|
2017-12-21 00:13:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 14:30:04 +00:00
|
|
|
/**
|
|
|
|
* A single example test.
|
|
|
|
*/
|
|
|
|
function test_add() {
|
2017-12-04 14:58:19 +00:00
|
|
|
$x = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
2017-12-03 20:33:16 +00:00
|
|
|
array(
|
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 'DESC'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Collections;
|
2017-11-03 20:23:41 +00:00
|
|
|
|
2017-12-03 20:33:16 +00:00
|
|
|
$this->assertEquals('Tainacan\Entities\Collection', get_class($x));
|
2017-11-23 00:30:48 +00:00
|
|
|
|
2017-12-03 20:33:16 +00:00
|
|
|
$test = $Tainacan_Collections->fetch($x->get_id());
|
2017-11-24 17:32:27 +00:00
|
|
|
|
2017-12-07 13:54:43 +00:00
|
|
|
$this->assertEquals('teste', $test->get_name());
|
|
|
|
$this->assertEquals('adasdasdsa', $test->get_description());
|
|
|
|
$this->assertEquals('DESC', $test->get_default_order());
|
2017-12-01 02:53:20 +00:00
|
|
|
$this->assertEquals('draft', $test->get_status());
|
2017-11-10 16:45:40 +00:00
|
|
|
}
|
2017-11-09 12:44:52 +00:00
|
|
|
|
|
|
|
function test_item() {
|
2017-12-04 14:58:19 +00:00
|
|
|
$x = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 'DESC'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Collections;
|
2017-12-04 14:58:19 +00:00
|
|
|
$collection = $Tainacan_Collections->fetch($x->get_id());
|
|
|
|
|
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item test',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'order' => 'DESC',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Items;
|
2017-12-04 14:58:19 +00:00
|
|
|
$item = $Tainacan_Items->fetch( $i->get_id() );
|
2017-11-09 12:44:52 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$this->assertEquals($item->get_title(), 'item test');
|
2017-11-09 12:44:52 +00:00
|
|
|
$this->assertEquals($item->get_description(), 'adasdasdsa');
|
2017-11-12 23:14:47 +00:00
|
|
|
$this->assertEquals($item->get_collection_id(), $collection->get_id());
|
2017-11-09 12:44:52 +00:00
|
|
|
|
2017-11-15 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_validation() {
|
2017-12-04 14:58:19 +00:00
|
|
|
$x = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'default_order' => 13
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-11-15 02:04:40 +00:00
|
|
|
$this->assertFalse($x->validate());
|
|
|
|
$this->assertTrue(sizeof($x->get_errors()) > 0);
|
|
|
|
|
2017-11-28 20:49:20 +00:00
|
|
|
$x->set_default_order('ASDASD');
|
|
|
|
|
|
|
|
$this->assertFalse($x->validate());
|
|
|
|
$this->assertTrue(sizeof($x->get_errors()) > 0);
|
|
|
|
|
|
|
|
$x->set_default_order('DESC');
|
2017-11-15 02:04:40 +00:00
|
|
|
$this->assertTrue($x->validate());
|
|
|
|
$this->assertTrue(empty($x->get_errors()));
|
2017-12-04 18:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_hooks() {
|
|
|
|
global $Tainacan_Collections;
|
|
|
|
$this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!');
|
2017-11-09 12:44:52 +00:00
|
|
|
}
|
2017-10-20 14:30:04 +00:00
|
|
|
}
|