Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* LocationTest.php - A test for the Location class.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @copyright 2017
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package tests
* @since 4.2
*/
require_once __DIR__ . '/../../../_bootstrap.php';
class LocationTest extends \Codeception\Test\Unit
{
protected $db_handle;
protected $oldUser;
protected function _before()
{
//First we must initialise the StudipPDO database connection:
$this->db_handle = new \StudipPDO(
'mysql:host='
. $GLOBALS['DB_STUDIP_HOST']
. ';dbname='
. $GLOBALS['DB_STUDIP_DATABASE'],
$GLOBALS['DB_STUDIP_USER'],
$GLOBALS['DB_STUDIP_PASSWORD']
);
//Then we must start a transaction before we access the database,
//otherwise we would spam the live database with test data!
$this->db_handle->beginTransaction();
//Now we tell the DBManager about the connection
//we have established to the Stud.IP database:
\DBManager::getInstance()->setConnection('studip', $this->db_handle);
// Workaround old-style Stud.IP-API using $GLOBALS['user']
$this->oldUser = $GLOBALS['user'];
$GLOBALS['user'] = new \Seminar_User(
\User::findByUsername('root@studip')
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
);
//As a final step we create the SORM objects for our test cases:
$this->location_cat = ResourceManager::createLocationCategory(
'TestLocation'
);
$this->location = $this->location_cat->createResource(
'Test location object'
);
//Everything is set up for the test cases.
}
protected function _after()
{
//We must roll back the changes we made in this test
//so that the live database remains unchanged after
//all the test cases of this test have been finished:
$this->db_handle->rollBack();
// Workaround old-style Stud.IP-API using $GLOBALS['user']
$GLOBALS['user'] = $this->oldUser;
}
public function testValidation()
{
$this->expectException(
InvalidResourceException::class
);
$invalid = new Location();
$invalid->name = 'invalid';
$invalid->parent_id = $this->location->id;
//The location is invalid since it has another resource
//as parent resource.
$invalid->store();
}
public function testGetURL()
{
$link = $this->location->getActionURL('show');
$this->assertEquals(
'dispatch.php/resources/location/index/' . $this->location->id,
$link
);
$link = $this->location->getActionURL('show', ['test' => '1']);
$this->assertEquals(
'dispatch.php/resources/location/index/' . $this->location->id . '?test=1',
$link
);
$link = $this->location->getActionURL('delete');
$this->assertEquals(
'dispatch.php/resources/location/delete/' . $this->location->id,
$link
);
}
}