Skip to content

Commit 27b4a03

Browse files
committed
test: added more tests for the class Faq
1 parent 29746c1 commit 27b4a03

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

phpmyfaq/src/phpMyFAQ/Faq.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,10 +1205,10 @@ public function isActive(int $recordId, string $recordLang, string $commentType
12051205

12061206
if ($row = $this->configuration->getDb()->fetchObject($result)) {
12071207
if (($row->active === 'y') || ($row->active === 'yes')) {
1208-
return false;
1208+
return true;
12091209
}
12101210
} else {
1211-
return true;
1211+
return false;
12121212
}
12131213
}
12141214

tests/phpMyFAQ/FaqTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace phpMyFAQ;
44

5+
use phpMyFAQ\Attachment\AttachmentException;
6+
use phpMyFAQ\Attachment\Filesystem\File\FileException;
57
use phpMyFAQ\Core\Exception;
68
use phpMyFAQ\Database\Sqlite3;
9+
use phpMyFAQ\Entity\FaqEntity;
710
use PHPUnit\Framework\TestCase;
811

912
class FaqTest extends TestCase
@@ -38,6 +41,18 @@ protected function setUp(): void
3841
$this->faq = new Faq($this->configuration);
3942
}
4043

44+
/**
45+
* @throws AttachmentException
46+
* @throws FileException
47+
*/
48+
protected function tearDown(): void
49+
{
50+
parent::tearDown();
51+
52+
$faqEntity = $this->getFaqEntity();
53+
$this->faq->deleteRecord(1, $faqEntity->getLanguage());
54+
}
55+
4156
public function testSetGroups(): void
4257
{
4358
$this->assertInstanceOf(Faq::class, $this->faq->setGroups([-1]));
@@ -53,4 +68,109 @@ public function testHasTitleAHash(): void
5368
$this->assertTrue($this->faq->hasTitleAHash('H#llo World!'));
5469
$this->assertFalse($this->faq->hasTitleAHash('Hallo World!'));
5570
}
71+
72+
public function testCreate(): void
73+
{
74+
$faqEntity = $this->getFaqEntity();
75+
76+
// Call the method being tested
77+
$result = $this->faq->create($faqEntity);
78+
79+
// Assert that the method returns an integer
80+
$this->assertIsInt($result);
81+
$this->assertGreaterThan(0, $result);
82+
}
83+
84+
public function testGetNextSolutionId(): void
85+
{
86+
$this->assertIsInt($this->faq->getNextSolutionId());
87+
$this->assertGreaterThan(0, $this->faq->getNextSolutionId());
88+
89+
$this->faq->create($this->getFaqEntity());
90+
91+
$this->assertGreaterThan(1, $this->faq->getNextSolutionId());
92+
}
93+
94+
public function testUpdate(): void
95+
{
96+
$faqEntity = $this->getFaqEntity();
97+
$faqEntity->setId($this->faq->create($faqEntity));
98+
99+
$faqEntity->setRevisionId(0);
100+
$faqEntity->setQuestion('Updated question');
101+
$faqEntity->setAnswer('Updated answer');
102+
103+
$result = $this->faq->update($faqEntity);
104+
105+
$this->assertTrue($result);
106+
}
107+
108+
/**
109+
* @throws AttachmentException
110+
* @throws FileException
111+
*/
112+
public function testDeleteRecord(): void
113+
{
114+
$faqEntity = $this->getFaqEntity();
115+
$faqEntity->setId($this->faq->create($faqEntity));
116+
117+
$result = $this->faq->deleteRecord($faqEntity->getId(), $faqEntity->getLanguage());
118+
119+
$this->assertTrue($result);
120+
}
121+
122+
public function testGetSolutionIdFromId(): void
123+
{
124+
$faqEntity = $this->getFaqEntity();
125+
$faqEntity->setId($this->faq->create($faqEntity));
126+
127+
$this->assertIsInt($this->faq->getSolutionIdFromId($faqEntity->getId(), $faqEntity->getLanguage()));
128+
$this->assertGreaterThan(0, $this->faq->getSolutionIdFromId($faqEntity->getId(), $faqEntity->getLanguage()));
129+
}
130+
131+
public function testHasTranslation(): void
132+
{
133+
$faqEntity = $this->getFaqEntity();
134+
$faqEntity->setId($this->faq->create($faqEntity));
135+
136+
$this->assertTrue($this->faq->hasTranslation($faqEntity->getId(), $faqEntity->getLanguage()));
137+
$this->assertFalse($this->faq->hasTranslation($faqEntity->getId(), 'de'));
138+
}
139+
140+
public function testIsActive(): void
141+
{
142+
$faqEntity = $this->getFaqEntity();
143+
$faqEntity->setId($this->faq->create($faqEntity));
144+
145+
$this->assertTrue($this->faq->isActive($faqEntity->getId(), $faqEntity->getLanguage()));
146+
}
147+
148+
public function testGetRecordBySolutionId(): void
149+
{
150+
$faqEntity = $this->getFaqEntity();
151+
$faqEntity->setSolutionId(42);
152+
$this->faq->create($faqEntity);
153+
154+
$this->faq->getRecordBySolutionId(42);
155+
156+
$this->assertEquals(1, $faqEntity->getId());
157+
}
158+
159+
private function getFaqEntity(): FaqEntity
160+
{
161+
$faqEntity = new FaqEntity();
162+
$faqEntity
163+
->setLanguage('en')
164+
->setActive(true)
165+
->setSticky(true)
166+
->setKeywords('Keywords')
167+
->setQuestion('Question')
168+
->setAnswer('Answer')
169+
->setAuthor('Author')
170+
->setEmail('[email protected]')
171+
->setComment(true)
172+
->setNotes('');
173+
174+
return $faqEntity;
175+
}
56176
}

0 commit comments

Comments
 (0)