12 namespace Fossology\Lib\Dao;
15 use PHPUnit\Framework\TestCase;
17 use PHPUnit\Runner\Version as PHPUnitVersion;
46 private $assertCountBefore;
50 protected function setUp() : void
52 $this->testDb =
new TestPgDb(
"licensestdcommentdao");
53 $this->
dbManager = $this->testDb->getDbManager();
55 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
56 $this->testDb->createPlainTables([
"users",
"license_std_comment"]);
57 $this->testDb->createSequences([
"license_std_comment_lsc_pk_seq"]);
58 $this->testDb->alterTables([
"license_std_comment"]);
59 $this->testDb->insertData([
"users",
"license_std_comment"]);
60 $this->authClass = \Mockery::mock(
'alias:Fossology\Lib\Auth\Auth');
61 $this->authClass->expects(
'getUserId')->andReturn(2);
64 protected function tearDown() : void
66 $this->addToAssertionCount(
67 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
81 $allComments = $this->licenseStdCommentDao->getAllComments();
82 $this->assertCount(self::COMMENTS_IN_DB, $allComments);
85 "name" =>
"Test comment #1",
86 "comment" =>
"This will be your first comment!",
89 $this->assertTrue(in_array($testData, $allComments),
90 'Missing test data 1 in DB.');
94 "comment" =>
"This comment is not set!",
97 $this->assertTrue(in_array($testData, $allComments),
98 'Missing test data 2 in DB.');
110 $filteredComments = $this->licenseStdCommentDao->getAllComments(
true);
111 $this->assertCount(self::COMMENTS_IN_DB - 1, $filteredComments);
114 "name" =>
"Test comment #1",
115 "comment" =>
"This will be your first comment!",
118 $this->assertTrue(in_array($testData, $filteredComments),
119 'Missing expected data in DB.');
123 "comment" =>
"This comment is not set!",
126 $this->assertFalse(in_array($testData, $filteredComments),
127 'Unexpected data returned for comments.');
139 $this->authClass->expects(
'isAdmin')->andReturn(
true);
141 $returnVal = $this->licenseStdCommentDao->updateComment(2,
142 "Updated comment #1",
"This comment is updated!");
143 $this->assertTrue($returnVal);
144 $sql =
"SELECT * FROM license_std_comment WHERE lsc_pk = 2;";
145 $row = $this->
dbManager->getSingleRow($sql);
146 $this->assertEquals(
"Updated comment #1", $row[
"name"]);
147 $this->assertEquals(
"This comment is updated!", $row[
"comment"]);
149 $pattern =
"/^" . date(
'Y-m-d') .
".*/";
150 if (intval(explode(
'.', PHPUnitVersion::id())[0]) >= 9) {
151 $this->assertMatchesRegularExpression($pattern, $row[
'updated']);
153 $this->assertRegExp($pattern, $row[
'updated']);
155 $this->assertEquals(2, $row[
'user_fk']);
167 $this->authClass->expects(
'isAdmin')->andReturn(
false);
169 $returnVal = $this->licenseStdCommentDao->updateComment(3,
170 "Updated comment #2",
"This comment is updated!");
171 $this->assertFalse($returnVal);
172 $sql =
"SELECT name, comment FROM license_std_comment WHERE lsc_pk = 3;";
173 $row = $this->
dbManager->getSingleRow($sql);
174 $this->assertNotEquals(
"Updated comment #2", $row[
"name"]);
175 $this->assertNotEquals(
"This comment is updated!", $row[
"comment"]);
186 $this->authClass->expects(
'isAdmin')->andReturn(
true);
188 $this->expectException(\UnexpectedValueException::class);
189 $this->licenseStdCommentDao->updateComment(- 1,
"Invalid comment #1",
190 "This comment is invalid!");
203 $this->authClass->expects(
'isAdmin')->andReturn(
true);
206 $newValues[3][
'name'] =
"Updated comment #2";
207 $newValues[3][
'comment'] =
"This comment is updated!";
208 $newValues[4][
'name'] =
"Updated comment #3";
209 $newValues[4][
'comment'] =
"This comment is updated!";
211 $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
213 $this->assertEquals(2, $returnVal);
214 $sql =
"SELECT * FROM license_std_comment WHERE lsc_pk IN (3, 4);";
217 foreach ($rows as $row) {
218 $this->assertEquals(
"Updated comment #" . ($id - 1), $row[
"name"]);
219 $this->assertEquals(
"This comment is updated!", $row[
"comment"]);
220 $pattern =
"/^" . date(
'Y-m-d') .
".*/";
221 if (intval(explode(
'.', PHPUnitVersion::id())[0]) >= 9) {
222 $this->assertMatchesRegularExpression($pattern, $row[
'updated']);
224 $this->assertRegExp($pattern, $row[
'updated']);
226 $this->assertEquals(2, $row[
'user_fk']);
240 $this->authClass->expects(
'isAdmin')->andReturn(
false);
243 $newValues[5][
'name'] =
"Updated comment #4";
244 $newValues[5][
'comment'] =
"This comment is updated!";
246 $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
248 $this->assertFalse($returnVal);
261 $this->authClass->expects(
'isAdmin')->andReturn(
true);
264 $newValues[-1][
'name'] =
"Updated comment #4";
265 $newValues[-1][
'comment'] =
"This comment is updated!";
267 $this->expectException(\UnexpectedValueException::class);
269 $this->licenseStdCommentDao->updateCommentFromArray($newValues);
282 $this->authClass->expects(
'isAdmin')->andReturn(
true);
285 $newValues[5][
'naaaaame'] =
"Updated comment #4";
286 $newValues[5][
'commmmmment'] =
"This comment is updated!";
288 $this->expectException(\UnexpectedValueException::class);
290 $this->licenseStdCommentDao->updateCommentFromArray($newValues);
303 $this->authClass->expects(
'isAdmin')->andReturn(
true);
307 $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
308 $this->assertEquals(0, $returnVal);
321 $this->authClass->expects(
'isAdmin')->andReturn(
true);
323 $newValues = [3 => []];
325 $this->expectException(\UnexpectedValueException::class);
327 $this->licenseStdCommentDao->updateCommentFromArray($newValues);
340 $returnVal = $this->licenseStdCommentDao->getComment($commentId);
342 $this->assertTrue(is_string($returnVal) || $returnVal ===
null);
343 if ($returnVal !==
null) {
344 $this->assertEquals(
"This will be the sixth comment!", $returnVal);
356 $this->expectException(\UnexpectedValueException::class);
359 $returnVal = $this->licenseStdCommentDao->getComment($commentId);
371 $this->authClass->expects(
'isAdmin')->andReturn(
true);
373 $returnVal = $this->licenseStdCommentDao->insertComment(
"Inserted comment #1",
374 "This first inserted comment!");
375 $this->assertTrue(is_numeric($returnVal));
376 $this->assertEquals(1, $returnVal);
377 $sql =
"SELECT * FROM license_std_comment WHERE lsc_pk = $1;";
378 $row = $this->
dbManager->getSingleRow($sql, [$returnVal]);
379 $this->assertEquals(
"Inserted comment #1", $row[
"name"]);
380 $this->assertEquals(
"This first inserted comment!", $row[
"comment"]);
381 $pattern =
"/^" . date(
'Y-m-d') .
".*/";
382 if (intval(explode(
'.', PHPUnitVersion::id())[0]) >= 9) {
383 $this->assertMatchesRegularExpression($pattern, $row[
'updated']);
385 $this->assertRegExp($pattern, $row[
'updated']);
387 $this->assertEquals(2, $row[
'user_fk']);
388 $this->assertEquals(
"t", $row[
"is_enabled"]);
399 $this->authClass->expects(
'isAdmin')->andReturn(
false);
401 $returnVal = $this->licenseStdCommentDao->insertComment(
"Inserted comment #1",
402 "This first inserted comment!");
403 $this->assertEquals(-1, $returnVal);
414 $this->authClass->expects(
'isAdmin')->andReturn(
true);
416 $returnVal = $this->licenseStdCommentDao->insertComment(
"",
418 $this->assertEquals(-1, $returnVal);
430 $this->authClass->expects(
'isAdmin')->andReturn(
true);
432 $returnVal = $this->licenseStdCommentDao->toggleComment(5);
433 $this->assertTrue($returnVal);
434 $sql =
"SELECT is_enabled FROM license_std_comment WHERE lsc_pk = 5;";
435 $row = $this->
dbManager->getSingleRow($sql);
436 $this->assertEquals(
"f", $row[
"is_enabled"]);
447 $this->authClass->expects(
'isAdmin')->andReturn(
false);
449 $returnVal = $this->licenseStdCommentDao->toggleComment(5);
450 $this->assertFalse($returnVal);
461 $this->authClass->expects(
'isAdmin')->andReturn(
true);
463 $this->expectException(\UnexpectedValueException::class);
466 $returnVal = $this->licenseStdCommentDao->toggleComment($commentId);
fo_dbManager * dbManager
fo_dbManager object