tainacan/tests/test-items.php

262 lines
7.8 KiB
PHP
Raw Normal View History

2017-11-12 23:14:47 +00:00
<?php
2017-11-15 18:50:11 +00:00
namespace Tainacan\Tests;
2017-11-12 23:14:47 +00:00
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
2017-12-04 18:20:49 +00:00
use Tainacan\Entities;
2017-11-29 18:06:22 +00:00
2017-11-12 23:14:47 +00:00
/**
* Sample test case.
*/
2017-12-04 18:20:49 +00:00
class Items extends TAINACAN_UnitTestCase {
2017-11-12 23:14:47 +00:00
2018-01-18 14:30:57 +00:00
/**
2018-02-09 18:59:57 +00:00
* @group permissions
2018-01-18 14:30:57 +00:00
*/
public function test_permissions () {
$collection = $this->tainacan_entity_factory->create_entity(
2018-01-18 14:30:57 +00:00
'collection',
array(
'name' => 'testePerm',
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItem',
'collection' => $collection,
),
true
);
$this->assertTrue($item->can_read(), 'Administrator cannot read the Item');
$this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item');
// another administrator should be able to edit items
$new_admin = $this->factory()->user->create(array( 'role' => 'administrator' ));
wp_set_current_user($new_admin);
$this->assertTrue($item->can_read(), 'Administrator cannot read the Item');
$this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item');
$this->assertTrue(current_user_can($collection->get_items_capabilities()->edit_post, $item->get_id()), 'Administrator cannot edit an item!');
2018-01-26 18:44:45 +00:00
$sub = $this->factory()->user->create(array( 'role' => 'subscriber', 'display_name' => 'Sub' ));
$collectionM = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testePermModerator',
'moderators_ids' => [$sub]
),
true
);
2018-01-26 18:44:45 +00:00
$itemM = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItemModerator',
'collection' => $collectionM,
),
true
);
$this->assertEquals([$sub], $collectionM->get_moderators_ids());
wp_set_current_user($sub);
$this->assertTrue(current_user_can($collectionM->get_items_capabilities()->edit_post, $itemM->get_id()), 'Moderators cannot edit an item!');
$this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit an item!');
2018-01-26 18:44:45 +00:00
2018-01-18 14:30:57 +00:00
}
2017-11-12 23:14:47 +00:00
function teste_query(){
2017-12-04 18:20:49 +00:00
$type = $this->tainacan_field_factory->create_field('text');
2017-11-12 23:14:47 +00:00
2017-12-04 18:20:49 +00:00
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'status' => 'publish'
),
true
);
2017-12-04 18:20:49 +00:00
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste2',
'status' => 'publish'
),
true
);
$field = $this->tainacan_entity_factory->create_entity(
'field',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
2017-12-04 18:20:49 +00:00
),
true
);
$field2 = $this->tainacan_entity_factory->create_entity(
'field',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado2',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
2017-12-04 18:20:49 +00:00
),
true
);
$field3 = $this->tainacan_entity_factory->create_entity(
'field',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado3',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
2017-12-04 18:20:49 +00:00
),
true
);
2017-11-12 23:14:47 +00:00
global $Tainacan_Items;
2017-12-04 18:20:49 +00:00
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'orange',
'collection' => $collection,
2018-01-29 13:09:44 +00:00
'status' => 'publish'
2017-12-04 18:20:49 +00:00
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field, 'value_1');
2017-12-04 18:20:49 +00:00
$item = $Tainacan_Items->fetch($i->get_id());
$meta_test = new Entities\Item_Metadata_Entity($item, $field);
$this->assertTrue( $meta_test instanceof Entities\Item_Metadata_Entity );
$this->assertEquals( $field->get_id(), $meta_test->get_field()->get_id() );
$this->assertEquals( 'value_1', $meta_test->get_value());
2017-12-04 18:20:49 +00:00
$i = $this->tainacan_entity_factory->create_entity(
2017-12-04 18:20:49 +00:00
'item',
array(
'title' => 'apple',
'collection' => $collection2,
2018-01-29 13:09:44 +00:00
'status' => 'publish'
2017-12-04 18:20:49 +00:00
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_2');
$i = $this->tainacan_entity_factory->create_entity(
2017-12-04 18:20:49 +00:00
'item',
array(
'title' => 'lemon',
'collection' => $collection2,
2018-01-29 13:09:44 +00:00
'status' => 'publish'
2017-12-04 18:20:49 +00:00
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_2');
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_3');
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_3');
2017-12-04 18:20:49 +00:00
$i = $this->tainacan_entity_factory->create_entity(
2017-12-04 18:20:49 +00:00
'item',
array(
'title' => 'pineapple',
'collection' => $collection2,
2018-01-29 13:09:44 +00:00
'status' => 'publish'
2017-12-04 18:20:49 +00:00
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_3');
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_6');
2017-12-04 18:20:49 +00:00
2017-11-12 23:14:47 +00:00
// should return all 4 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([]);
2017-11-29 18:06:22 +00:00
$this->assertEquals(4, $test_query->post_count );
2017-11-12 23:14:47 +00:00
// should also return all 4 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([], [$collection, $collection2]);
2017-11-29 18:06:22 +00:00
$this->assertEquals(4, $test_query->post_count);
2017-11-12 23:14:47 +00:00
2017-11-30 16:42:11 +00:00
// should return 2 items
$test_query = $Tainacan_Items->fetch(['posts_per_page' => 2], [$collection, $collection2]);
$this->assertEquals(2, $test_query->post_count);
2017-11-12 23:14:47 +00:00
// should return only the first item
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([], $collection);
2017-11-29 18:06:22 +00:00
$this->assertEquals(1,$test_query->post_count);
$test_query->the_post();
2017-12-04 18:20:49 +00:00
$item1 = new Entities\Item( get_the_ID() );
$this->assertEquals('orange', $item1->get_title() );
2017-11-29 18:06:22 +00:00
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch(['title' => 'orange']);
2017-11-29 18:06:22 +00:00
$test_query->the_post();
2017-12-04 18:20:49 +00:00
$item2 = new Entities\Item( get_the_ID() );
2017-11-29 18:06:22 +00:00
$this->assertEquals(1, $test_query->post_count);
2017-12-04 18:20:49 +00:00
$this->assertEquals('orange', $item2->get_title());
2017-11-12 23:14:47 +00:00
// should return the other 3 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(3,$test_query->post_count);
2017-11-12 23:14:47 +00:00
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch(['title' => 'apple']);
2017-11-29 18:06:22 +00:00
$test_query->the_post();
2017-12-04 18:20:49 +00:00
$item3 = new Entities\Item( get_the_ID() );
2017-11-29 18:06:22 +00:00
$this->assertEquals(1, $test_query->post_count);
2017-12-04 18:20:49 +00:00
$this->assertEquals('apple', $item3->get_title());
2017-11-12 23:14:47 +00:00
// should return 1 item
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
2017-11-12 23:14:47 +00:00
[
'key' => $field2->get_id(),
2017-11-12 23:14:47 +00:00
'value' => 'value_2'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(1, $test_query->post_count);
2017-11-12 23:14:47 +00:00
// should return 2 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
2017-11-12 23:14:47 +00:00
[
'key' => $field2->get_id(),
2017-11-12 23:14:47 +00:00
'value' => 'value_3'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(2, $test_query->post_count);
2017-11-12 23:14:47 +00:00
// should return 2 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
2017-11-12 23:14:47 +00:00
[
'key' => $field3->get_id(),
2017-11-12 23:14:47 +00:00
'value' => 'value_2',
'compare' => '>'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(2, $test_query->post_count);
2017-11-12 23:14:47 +00:00
}
}