1<?php
2
3/**
4 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
5 * all the essential functionalities required for any enterprise.
6 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
7 *
8 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
9 * the GNU General Public License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with this program;
17 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA  02110-1301, USA
19 *
20 */
21
22/**
23 * Test class for home page dao
24 */
25class HomePageDaoTest extends PHPUnit_Framework_TestCase {
26
27    private $homePageDao;
28    private $fixture;
29    private $testData;
30
31    /**
32     * Set up method
33     */
34    protected function setUp() {
35
36        TestDataService::truncateTables(array('ModuleDefaultPage', 'HomePage', 'UserRole', 'Module'));
37        $this->fixture = sfConfig::get('sf_plugins_dir') . '/orangehrmCorePlugin/test/fixtures/HomePageDao.yml';
38        $this->testData = sfYaml::load($this->fixture);
39        TestDataService::populate($this->fixture);
40        $this->homePageDao = new HomePageDao();
41    }
42
43    public function testGetHomePagesInPriorityOrderOneRole() {
44        $homePagesFixture = $this->testData['HomePage'];
45        $expected = array($homePagesFixture[3], $homePagesFixture[2], $homePagesFixture[0]);
46        $homePages = $this->homePageDao->getHomePagesInPriorityOrder(array(1));
47        $this->compareHomePages($expected, $homePages);
48
49        $expected = array($homePagesFixture[1]);
50        $homePages = $this->homePageDao->getHomePagesInPriorityOrder(array(2));
51        $this->compareHomePages($expected, $homePages);
52
53        $expected = array($homePagesFixture[4]);
54        $homePages = $this->homePageDao->getHomePagesInPriorityOrder(array(3));
55        $this->compareHomePages($expected, $homePages);
56
57    }
58
59    public function testGetHomePagesInPriorityOrderMultipleRole() {
60        $homePagesFixture = $this->testData['HomePage'];
61        $expected = array($homePagesFixture[3], $homePagesFixture[4], $homePagesFixture[2], $homePagesFixture[0], $homePagesFixture[1]);
62        $homePages = $this->homePageDao->getHomePagesInPriorityOrder(array(1, 2, 3));
63        $this->compareHomePages($expected, $homePages);
64    }
65
66    /**
67     * Test case for no matching home pages for user role
68     */
69    public function testGetHomePagesInPriorityOrderNoMatches() {
70        $homePages = $this->homePageDao->getHomePagesInPriorityOrder(array(4));
71        $this->assertEquals(0, count($homePages));
72    }
73
74    /**
75     * Test case for no matching home pages for user role
76     */
77    public function testGetHomePagesInPriorityNoUserRoles() {
78        $homePages = $this->homePageDao->getHomePagesInPriorityOrder(array());
79        $this->assertEquals(0, count($homePages));
80    }
81
82    protected function compareHomePages($expected, $result) {
83        $this->assertEquals(count($expected), count($result));
84
85        for($i = 0; $i < count($expected); $i++) {
86            $exp = $expected[$i];
87            $res = $result[$i];
88
89            $this->assertEquals($exp['id'], $res->getId());
90            $this->assertEquals($exp['user_role_id'], $res->getUserRoleId());
91            $this->assertEquals($exp['action'], $res->getAction());
92            $this->assertEquals($exp['enable_class'], $res->getEnableClass());
93            $this->assertEquals($exp['priority'], $res->getPriority());
94        }
95    }
96
97    public function testGetModuleDefaultPagesInPriorityOrderOneRole() {
98        $pagesFixture = $this->testData['ModuleDefaultPage'];
99        $expected = array($pagesFixture[7], $pagesFixture[4]);
100        $homePages = $this->homePageDao->getModuleDefaultPagesInPriorityOrder('leave', array(1));
101        $this->compareModuleDefaultPages($expected, $homePages);
102
103        $expected = array($pagesFixture[3]);
104        $homePages = $this->homePageDao->getModuleDefaultPagesInPriorityOrder('pim', array(2));
105        $this->compareModuleDefaultPages($expected, $homePages);
106
107        $expected = array($pagesFixture[5]);
108        $homePages = $this->homePageDao->getModuleDefaultPagesInPriorityOrder('leave', array(3));
109        $this->compareModuleDefaultPages($expected, $homePages);
110
111    }
112
113    public function testGetModuleDefaultPagesInPriorityOrderMultipleRole() {
114        $pagesFixture = $this->testData['ModuleDefaultPage'];
115        $expected = array($pagesFixture[7], $pagesFixture[8], $pagesFixture[4], $pagesFixture[5], $pagesFixture[6]);
116        $homePages = $this->homePageDao->getModuleDefaultPagesInPriorityOrder('leave', array(1, 2, 3));
117        $this->compareModuleDefaultPages($expected, $homePages);
118    }
119
120    /**
121     * Test case for no matching home pages for user role
122     */
123    public function testGetModuleDefaultPagesInPriorityOrderNoMatches() {
124        $pagesFixture = $this->homePageDao->getModuleDefaultPagesInPriorityOrder('leave', array(4));
125        $this->assertEquals(0, count($pagesFixture));
126    }
127
128    /**
129     * Test case for no matching home pages for user role
130     */
131    public function xtestGetModuleDefaultPagesInPriorityNoUserRoles() {
132        $pagesFixture = $this->homePageDao->getModuleDefaultPagesInPriorityOrder(array());
133        $this->assertEquals(0, count($pagesFixture));
134    }
135
136    protected function compareModuleDefaultPages($expected, $result) {
137        $this->assertEquals(count($expected), count($result));
138
139        for($i = 0; $i < count($expected); $i++) {
140            $exp = $expected[$i];
141            $res = $result[$i];
142
143            $this->assertEquals($exp['id'], $res->getId());
144            $this->assertEquals($exp['module_id'], $res->getModuleId());
145            $this->assertEquals($exp['user_role_id'], $res->getUserRoleId());
146            $this->assertEquals($exp['action'], $res->getAction());
147            $this->assertEquals($exp['enable_class'], $res->getEnableClass());
148            $this->assertEquals($exp['priority'], $res->getPriority());
149        }
150    }
151}
152