54 private $assertCountBefore;
58 protected function setUp()
60 $this->testDb =
new TestPgDb(
"licensestdcommentdao");
61 $this->
dbManager = $this->testDb->getDbManager();
63 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
64 $this->testDb->createPlainTables([
"users",
"license_std_comment"]);
65 $this->testDb->createSequences([
"license_std_comment_lsc_pk_seq"]);
66 $this->testDb->alterTables([
"license_std_comment"]);
67 $this->testDb->insertData([
"users",
"license_std_comment"]);
68 $this->authClass = \Mockery::mock(
'alias:Fossology\Lib\Auth\Auth');
69 $this->authClass->expects(
'getUserId')->andReturn(2);
72 protected function tearDown()
74 $this->addToAssertionCount(
75 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
89 $allComments = $this->licenseStdCommentDao->getAllComments();
90 $this->assertCount(self::COMMENTS_IN_DB, $allComments);
91 $this->assertContains([
93 "name" =>
"Test comment #1",
94 "comment" =>
"This will be your first comment!",
96 ], $allComments,
false);
97 $this->assertContains([
100 "comment" =>
"This comment is not set!",
102 ], $allComments,
false);
114 $filteredComments = $this->licenseStdCommentDao->getAllComments(
true);
115 $this->assertCount(self::COMMENTS_IN_DB - 1, $filteredComments);
116 $this->assertContains([
118 "name" =>
"Test comment #1",
119 "comment" =>
"This will be your first comment!",
121 ], $filteredComments,
false);
122 $this->assertNotContains([
125 "comment" =>
"This comment is not set!",
126 "is_enabled" =>
false 127 ], $filteredComments);
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"]);
148 $this->assertRegExp(
"/^" . date(
'Y-m-d') .
".*/", $row[
'updated']);
149 $this->assertEquals(2, $row[
'user_fk']);
161 $this->authClass->expects(
'isAdmin')->andReturn(
false);
163 $returnVal = $this->licenseStdCommentDao->updateComment(3,
164 "Updated comment #2",
"This comment is updated!");
165 $this->assertFalse($returnVal);
166 $sql =
"SELECT name, comment FROM license_std_comment WHERE lsc_pk = 3;";
167 $row = $this->
dbManager->getSingleRow($sql);
168 $this->assertNotEquals(
"Updated comment #2", $row[
"name"]);
169 $this->assertNotEquals(
"This comment is updated!", $row[
"comment"]);
180 $this->authClass->expects(
'isAdmin')->andReturn(
true);
182 $this->expectException(\UnexpectedValueException::class);
183 $this->licenseStdCommentDao->updateComment(- 1,
"Invalid comment #1",
184 "This comment is invalid!");
197 $this->authClass->expects(
'isAdmin')->andReturn(
true);
200 $newValues[3][
'name'] =
"Updated comment #2";
201 $newValues[3][
'comment'] =
"This comment is updated!";
202 $newValues[4][
'name'] =
"Updated comment #3";
203 $newValues[4][
'comment'] =
"This comment is updated!";
205 $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
207 $this->assertEquals(2, $returnVal);
208 $sql =
"SELECT * FROM license_std_comment WHERE lsc_pk IN (3, 4);";
211 foreach ($rows as $row) {
212 $this->assertEquals(
"Updated comment #" . ($id - 1), $row[
"name"]);
213 $this->assertEquals(
"This comment is updated!", $row[
"comment"]);
214 $this->assertRegExp(
"/^" . date(
'Y-m-d') .
".*/", $row[
'updated']);
215 $this->assertEquals(2, $row[
'user_fk']);
229 $this->authClass->expects(
'isAdmin')->andReturn(
false);
232 $newValues[5][
'name'] =
"Updated comment #4";
233 $newValues[5][
'comment'] =
"This comment is updated!";
235 $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
237 $this->assertFalse($returnVal);
250 $this->authClass->expects(
'isAdmin')->andReturn(
true);
253 $newValues[-1][
'name'] =
"Updated comment #4";
254 $newValues[-1][
'comment'] =
"This comment is updated!";
256 $this->expectException(\UnexpectedValueException::class);
258 $this->licenseStdCommentDao->updateCommentFromArray($newValues);
271 $this->authClass->expects(
'isAdmin')->andReturn(
true);
274 $newValues[5][
'naaaaame'] =
"Updated comment #4";
275 $newValues[5][
'commmmmment'] =
"This comment is updated!";
277 $this->expectException(\UnexpectedValueException::class);
279 $this->licenseStdCommentDao->updateCommentFromArray($newValues);
292 $this->authClass->expects(
'isAdmin')->andReturn(
true);
296 $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
297 $this->assertEquals(0, $returnVal);
310 $this->authClass->expects(
'isAdmin')->andReturn(
true);
312 $newValues = [3 => []];
314 $this->expectException(\UnexpectedValueException::class);
316 $this->licenseStdCommentDao->updateCommentFromArray($newValues);
329 $returnVal = $this->licenseStdCommentDao->getComment($commentId);
331 $this->assertTrue(is_string($returnVal) || $returnVal === null);
332 if ($returnVal !== null) {
333 $this->assertEquals(
"This will be the sixth comment!", $returnVal);
345 $this->expectException(\UnexpectedValueException::class);
348 $returnVal = $this->licenseStdCommentDao->getComment($commentId);
360 $this->authClass->expects(
'isAdmin')->andReturn(
true);
362 $returnVal = $this->licenseStdCommentDao->insertComment(
"Inserted comment #1",
363 "This first inserted comment!");
364 $this->assertTrue(is_numeric($returnVal));
365 $this->assertEquals(1, $returnVal);
366 $sql =
"SELECT * FROM license_std_comment WHERE lsc_pk = $1;";
367 $row = $this->
dbManager->getSingleRow($sql, [$returnVal]);
368 $this->assertEquals(
"Inserted comment #1", $row[
"name"]);
369 $this->assertEquals(
"This first inserted comment!", $row[
"comment"]);
370 $this->assertRegExp(
"/^" . date(
'Y-m-d') .
".*/", $row[
'updated']);
371 $this->assertEquals(2, $row[
'user_fk']);
372 $this->assertEquals(
"t", $row[
"is_enabled"]);
383 $this->authClass->expects(
'isAdmin')->andReturn(
false);
385 $returnVal = $this->licenseStdCommentDao->insertComment(
"Inserted comment #1",
386 "This first inserted comment!");
387 $this->assertEquals(-1, $returnVal);
398 $this->authClass->expects(
'isAdmin')->andReturn(
true);
400 $returnVal = $this->licenseStdCommentDao->insertComment(
"",
402 $this->assertEquals(-1, $returnVal);
414 $this->authClass->expects(
'isAdmin')->andReturn(
true);
416 $returnVal = $this->licenseStdCommentDao->toggleComment(5);
417 $this->assertTrue($returnVal);
418 $sql =
"SELECT is_enabled FROM license_std_comment WHERE lsc_pk = 5;";
419 $row = $this->
dbManager->getSingleRow($sql);
420 $this->assertEquals(
"f", $row[
"is_enabled"]);
431 $this->authClass->expects(
'isAdmin')->andReturn(
false);
433 $returnVal = $this->licenseStdCommentDao->toggleComment(5);
434 $this->assertFalse($returnVal);
445 $this->authClass->expects(
'isAdmin')->andReturn(
true);
447 $this->expectException(\UnexpectedValueException::class);
450 $returnVal = $this->licenseStdCommentDao->toggleComment($commentId);
fo_dbManager * dbManager
fo_dbManager object