tainacan/tests/test-collections.php

274 lines
7.4 KiB
PHP
Raw Normal View History

<?php
2017-11-15 18:50:11 +00:00
namespace Tainacan\Tests;
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class Collections extends TAINACAN_UnitTestCase {
2017-12-21 00:13:33 +00:00
/**
* @group permissions
*/
function test_permissions () {
2019-11-04 21:55:40 +00:00
global $current_user;
$collection_test = $this->tainacan_entity_factory->create_entity(
'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);
2019-11-04 21:55:40 +00:00
$autor1 = $this->factory()->user->create(array( 'role' => 'author' ));
wp_set_current_user($autor1);
$autor1_id = get_current_user_id();
$collection_test = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeCapsOwner',
'description' => 'adasdasdsa',
'default_order' => 'DESC'
),
true
);
$this->assertEquals($autor1_id, $collection_test->WP_Post->post_author);
$autor2 = $this->factory()->user->create(array( 'role' => 'author', 'display_name' => 'Author2' ));
wp_set_current_user($autor2);
$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
);
2019-11-04 21:55:40 +00:00
$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();
$this->assertEquals($autor2, $current_user_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->assertFalse(user_can($autor2, $collection_test->cap->edit_post, $collection_test->WP_Post->ID));
2019-11-04 21:55:40 +00:00
}
2019-11-04 21:55:40 +00:00
function debug_meta($user = false)
{
if($user !== false) wp_set_current_user($user);
$data = get_userdata( get_current_user_id() );
2019-11-04 21:55:40 +00:00
if ( is_object( $data) ) {
$current_user_caps = $data->allcaps;
2019-11-04 21:55:40 +00:00
// 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
}
2019-11-04 21:55:40 +00:00
/**
* A single example test.
*/
function test_add() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC'
),
true
);
2018-04-11 14:18:55 +00:00
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
2019-11-04 21:55:40 +00:00
$this->assertEquals('Tainacan\Entities\Collection', get_class($x));
2019-11-04 21:55:40 +00:00
$test = $Tainacan_Collections->fetch($x->get_id());
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());
$this->assertEquals('draft', $test->get_status());
2017-11-10 16:45:40 +00:00
}
2019-11-04 21:55:40 +00:00
function test_unique_slugs() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'slug' => 'duplicated_slug',
'status' => 'publish'
),
true
);
2019-11-04 21:55:40 +00:00
$y = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'slug' => 'duplicated_slug',
'status' => 'publish'
),
true
);
2019-11-04 21:55:40 +00:00
$this->assertNotEquals($x->get_slug(), $y->get_slug());
2019-11-04 21:55:40 +00:00
// Create as draft and publish later
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'slug' => 'duplicated_slug',
),
true
);
2019-11-04 21:55:40 +00:00
$y = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'slug' => 'duplicated_slug',
),
true
);
2019-11-04 21:55:40 +00:00
$this->assertEquals($x->get_slug(), $y->get_slug());
2019-11-04 21:55:40 +00:00
2018-04-11 14:18:55 +00:00
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$x->set_status('publish');
$x->validate();
$x = $Tainacan_Collections->insert($x);
$y->set_status('private'); // or publish shoud behave the same
$y->validate();
$y = $Tainacan_Collections->insert($y);
2019-11-04 21:55:40 +00:00
$this->assertNotEquals($x->get_slug(), $y->get_slug());
2019-11-04 21:55:40 +00:00
}
2019-11-04 21:55:40 +00:00
2017-11-09 12:44:52 +00:00
function test_item() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC'
),
true
);
2018-04-11 14:18:55 +00:00
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$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
);
2018-04-11 14:18:55 +00:00
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$item = $Tainacan_Items->fetch( $i->get_id() );
2019-11-04 21:55:40 +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());
2019-11-04 21:55:40 +00:00
2017-11-15 02:04:40 +00:00
}
2019-11-04 21:55:40 +00:00
2017-11-15 02:04:40 +00:00
function test_validation() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
2018-02-09 16:28:12 +00:00
'default_order' => 13,
'status' => 'publish'
)
);
2017-11-15 02:04:40 +00:00
$this->assertFalse($x->validate());
$this->assertTrue(sizeof($x->get_errors()) > 0);
2019-11-04 21:55:40 +00:00
2017-11-28 20:49:20 +00:00
$x->set_default_order('ASDASD');
2019-11-04 21:55:40 +00:00
2017-11-28 20:49:20 +00:00
$this->assertFalse($x->validate());
$this->assertTrue(sizeof($x->get_errors()) > 0);
2019-11-04 21:55:40 +00:00
2017-11-28 20:49:20 +00:00
$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
}
2019-11-04 21:55:40 +00:00
2017-12-04 18:47:38 +00:00
function test_hooks() {
2018-04-11 14:18:55 +00:00
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
2017-12-04 18:47:38 +00:00
$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
}
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
function test_create_child_collection() {
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
),
true
);
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$col = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'status' => 'auto-draft'
),
true
);
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$Collections = \Tainacan\Repositories\Collections::get_instance();
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$col->set_parent($x->get_id());
$col->set_status('publish');
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$col->validate();
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$col = $Collections->insert($col);
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
$this->assertEquals($x->get_id(), $col->get_parent());
2019-11-04 21:55:40 +00:00
2019-03-01 19:24:07 +00:00
}
}