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
<?php
use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;
use WoohooLabs\Yang\JsonApi\Schema\Document;
use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;
// Required for consultation mailer
require_once 'vendor/flexi/flexi.php';
trait ConsultationHelper
{
/**
* @var \UnitTester
*/
protected $tester;
protected function _before()
{
\DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
}
protected static $BLOCK_DATA = [
'room' => 'Testraum',
'calendar_events' => false,
'show_participants' => false,
'require_reason' => 'no',
'confirmation_text' => null,
'note' => 'Testnotiz für Block',
'size' => 1,
];
protected static $SLOT_DATA = [
'note' => 'Testnotiz für Slot',
];
protected static $BOOKING_DATA = [
'reason' => 'Test reason',
];
protected function getUserForCredentials(array $credentials): User
{
return User::find($credentials['id']);
}
protected function createBlockWithSlotsForRange(Range $range, bool $lock_blocks = false): ConsultationBlock
$slot_length_in_hours = 2;
// Generate start and end time. Assures that the day is not a holiday.
$now = time();
do {
$begin = strtotime('next monday 8:00:00', $now);
$end = strtotime("+{$slot_length_in_hours} hours", $begin);
$now = strtotime('+1 week', $now);
$temp = holiday($begin);
} while (is_array($temp) && $temp['col'] === 3);
// Lock blocks?
$additional_data = [];
if ($lock_blocks) {
$additional_data['lock_time'] = ceil(($begin - time()) / 3600) + $slot_length_in_hours;
$blocks = ConsultationBlock::generateBlocks(
$range,
1
);
$blocks = iterator_to_array($blocks);
$block = reset($blocks);
$block->setData(array_merge(self::$BLOCK_DATA, $additional_data));

Jan-Hendrik Willms
committed
$block->slots->exchangeArray($block->createSlots(15));
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
foreach ($block->slots as $slot) {
$slot->setData(self::$SLOT_DATA['note']);
}
$block->store();
return ConsultationBlock::find($block->id);
}
protected function getSlotFromBlock(ConsultationBlock $block): ConsultationSlot
{
return $block->slots->first();
}
protected function withStudipEnv(array $credentials, callable $fn)
{
// Create global template factory if neccessary
$has_template_factory = isset($GLOBALS['template_factory']);
if (!$has_template_factory) {
$GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
}
$result = $this->tester->withPHPLib($credentials, $fn);
if (!$has_template_factory) {
unset($GLOBALS['template_factory']);
}
return $result;
}
protected function createBookingForSlot(array $credentials, ConsultationSlot $slot, User $user): ConsultationBooking
{
return $this->withStudipEnv(
$credentials,
function () use ($slot, $user): ConsultationBooking {
$booking = new ConsultationBooking();
$booking->slot_id = $slot->id;
$booking->user_id = $user->id;
$booking->setData(self::$BOOKING_DATA);
$booking->store();
return $booking;
}
);
}
protected function sendMockRequest(string $route, string $handler, array $credentials, array $variables = [], array $options = []): JsonApiResponse
{
$options = array_merge([
'method' => 'GET',
'considered_successful' => [200],
'json_body' => null,
], $options);
$app = $this->tester->createApp(
$credentials,
strtolower($options['method']),
$route,
$handler
);
$evaluated_route = preg_replace_callback(
'/\{(.+?)(:[^}]+)?}/',
function ($match) use ($variables) {
$key = $match[1];
if (!isset($variables[$key])) {
throw new Exception("No variable '{$key}' defined");
}
return $variables[$key];
},
$route
);
$requestBuilder = $this->tester->createRequestBuilder($credentials);
$requestBuilder->setUri($evaluated_route)->setMethod(strtoupper($options['method']));
if (isset($options['json_body'])) {
$requestBuilder->setJsonApiBody($options['json_body']);
}
/** @var JsonApiResponse $response */
$response = $this->withStudipEnv($credentials, function () use ($app, $requestBuilder) {
return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
});
if ($options['considered_successful']) {
$this->assertTrue(
$response->isSuccessful($options['considered_successful']),
'Actual status code is ' . $response->getStatusCode()
);
}
return $response;
}
protected function getSingleResourceDocument(JsonApiResponse $response): Document
{
$this->assertTrue($response->hasDocument());
$document = $response->document();
$this->assertTrue($document->isSingleResourceDocument());
return $document;
}
protected function getResourceCollectionDocument(JsonApiResponse $response): Document
{
$this->assertTrue($response->hasDocument());
$document = $response->document();
$this->assertTrue($document->isResourceCollectionDocument());
return $document;
}
protected function assertHasRelations(ResourceObject $resource, ...$relations)
{
foreach ($relations as $relation) {
$this->assertTrue($resource->hasRelationship($relation));
}
}
}