#8212: can't sort by tags
[tine20] / tests / tine20 / Tinebase / ModelConfigurationTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Tinebase
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Alexander Stintzing <a.stintzing@metaways.de>
9  */
10
11 /**
12  * Test helper
13  */
14 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
15
16 /**
17  * Test class for Tinebase_ModelConfiguration, using the test class from hr
18  */
19 class Tinebase_ModelConfigurationTest extends PHPUnit_Framework_TestCase
20 {
21     /**
22      * tests if the modelconfiguration gets created using the static call
23      */
24     public function testModelCreation()
25     {
26         $employeeCObj = HumanResources_Model_Employee::getConfiguration();
27         $fields = $employeeCObj->getFields();
28
29         // test modlog field
30         $this->assertArrayHasKey('deleted_time', $fields);
31         // test supervisor_id field
32         $this->assertArrayHasKey('supervisor_id', $fields);
33         $this->assertArrayHasKey('notes', $fields);
34         $this->assertArrayHasKey('tags', $fields);
35
36         $contractCObj = HumanResources_Model_Contract::getConfiguration();
37         $fields = $contractCObj->getFields();
38
39         // test supervisor_id field not existing in contact configuration object
40         $this->assertArrayNotHasKey('supervisor_id', $fields);
41         $this->assertArrayHasKey('employee_id', $fields);
42
43         // test employee config again, so nothing gets overwritten
44         $employeeCObj = HumanResources_Model_Employee::getConfiguration();
45         $fields = $employeeCObj->getFields();
46         $this->assertArrayHasKey('deleted_time',  $fields);
47         $this->assertArrayHasKey('supervisor_id', $fields);
48         $this->assertArrayHasKey('id',            $fields);
49         
50         $account = Tinebase_Core::getUser();
51
52         $employee = new HumanResources_Model_Employee(array(
53             'account_id' => $account->getId(),
54             'n_family'   => $account->accountLastName,
55             'n_given'    => $account->accountFirstName,
56         ));
57         
58         // test record fields
59         $modelConfig = $employee::getConfiguration();
60         
61         $resolveFields = $modelConfig->recordFields;
62         $this->assertArrayHasKey('account_id',       $resolveFields);
63         $this->assertArrayHasKey('division_id',      $resolveFields);
64         $this->assertArrayHasKey('created_by',       $resolveFields);
65         $this->assertArrayHasKey('last_modified_by', $resolveFields);
66         $this->assertArrayHasKey('deleted_by',       $resolveFields);
67
68         $contact = new Addressbook_Model_Contact(array('n_family' => 'Spencer', 'n_given' => 'Bud'));
69         $co = $contact::getConfiguration();
70         $this->assertNull($co);
71         
72         // test the created filter model
73         $filterModel = $employee::getConfiguration()->filterModel;
74         
75         $this->assertArrayHasKey('id',               $filterModel);
76         $this->assertArrayHasKey('query',            $filterModel);
77         $this->assertArrayHasKey('account_id',       $filterModel);
78         $this->assertArrayHasKey('supervisor_id',    $filterModel);
79         
80         $this->assertArrayHasKey('options',          $filterModel['supervisor_id']);
81         $this->assertArrayHasKey('controller',       $filterModel['supervisor_id']['options']);
82         $this->assertArrayHasKey('filtergroup',      $filterModel['supervisor_id']['options']);
83         
84         $this->assertArrayHasKey('division_id',      $filterModel);
85         $this->assertArrayHasKey('created_by',       $filterModel);
86         $this->assertArrayHasKey('last_modified_by', $filterModel);
87         $this->assertArrayHasKey('deleted_by',       $filterModel);
88         
89         $this->assertArrayNotHasKey('employee_id',    $filterModel);
90         
91         $this->assertEquals('Tinebase_Model_Filter_Query', $filterModel['query']['filter']);
92     }
93
94     /**
95      * tests if the modelconfiguration gets created using the instance call
96      */
97     public function testModelCreationByInstance()
98     {
99         // test instance
100         $account = Tinebase_Core::getUser();
101
102         $employee = new HumanResources_Model_Employee(array(
103             'account_id' => $account->getId(),
104             'n_family' => $account->accountLastName,
105             'n_given' => $account->accountFirstName,
106         ));
107
108         $employeeCObj = $employee::getConfiguration();
109         $fields = $employeeCObj->getFields();
110         $this->assertArrayHasKey('deleted_time', $fields);
111         $this->assertArrayHasKey('supervisor_id', $fields);
112         
113         // test if sortable is set to false on tags-field
114         $this->assertArrayHasKey('sortable', $fields['tags']);
115         $this->assertFalse($fields['tags']['sortable']);
116         
117         $this->assertEquals($account->accountLastName, $employee->n_family);
118         $this->assertEquals($account->accountFirstName, $employee->n_given);
119     }
120
121     /**
122      * tests if the modelconfiguration gets created for the traditional models
123      */
124     public function testModelCreationTraditional()
125     {
126         $contact = new Addressbook_Model_Contact(array('n_family' => 'Spencer', 'n_given' => 'Bud'));
127         $cObj = $contact->getConfiguration();
128
129         // at first this is just null
130         $this->assertNull($cObj);
131     }
132 }