Skip to content

Commit

Permalink
test: added more tests for the class Faq
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 23, 2024
1 parent 29746c1 commit 27b4a03
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
4 changes: 2 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,10 @@ public function isActive(int $recordId, string $recordLang, string $commentType

if ($row = $this->configuration->getDb()->fetchObject($result)) {
if (($row->active === 'y') || ($row->active === 'yes')) {
return false;
return true;
}
} else {
return true;
return false;
}
}

Expand Down
120 changes: 120 additions & 0 deletions tests/phpMyFAQ/FaqTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace phpMyFAQ;

use phpMyFAQ\Attachment\AttachmentException;
use phpMyFAQ\Attachment\Filesystem\File\FileException;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Entity\FaqEntity;
use PHPUnit\Framework\TestCase;

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

/**
* @throws AttachmentException
* @throws FileException
*/
protected function tearDown(): void
{
parent::tearDown();

$faqEntity = $this->getFaqEntity();
$this->faq->deleteRecord(1, $faqEntity->getLanguage());
}

public function testSetGroups(): void
{
$this->assertInstanceOf(Faq::class, $this->faq->setGroups([-1]));
Expand All @@ -53,4 +68,109 @@ public function testHasTitleAHash(): void
$this->assertTrue($this->faq->hasTitleAHash('H#llo World!'));
$this->assertFalse($this->faq->hasTitleAHash('Hallo World!'));
}

public function testCreate(): void
{
$faqEntity = $this->getFaqEntity();

// Call the method being tested
$result = $this->faq->create($faqEntity);

// Assert that the method returns an integer
$this->assertIsInt($result);
$this->assertGreaterThan(0, $result);
}

public function testGetNextSolutionId(): void
{
$this->assertIsInt($this->faq->getNextSolutionId());
$this->assertGreaterThan(0, $this->faq->getNextSolutionId());

$this->faq->create($this->getFaqEntity());

$this->assertGreaterThan(1, $this->faq->getNextSolutionId());
}

public function testUpdate(): void
{
$faqEntity = $this->getFaqEntity();
$faqEntity->setId($this->faq->create($faqEntity));

$faqEntity->setRevisionId(0);
$faqEntity->setQuestion('Updated question');
$faqEntity->setAnswer('Updated answer');

$result = $this->faq->update($faqEntity);

$this->assertTrue($result);
}

/**
* @throws AttachmentException
* @throws FileException
*/
public function testDeleteRecord(): void
{
$faqEntity = $this->getFaqEntity();
$faqEntity->setId($this->faq->create($faqEntity));

$result = $this->faq->deleteRecord($faqEntity->getId(), $faqEntity->getLanguage());

$this->assertTrue($result);
}

public function testGetSolutionIdFromId(): void
{
$faqEntity = $this->getFaqEntity();
$faqEntity->setId($this->faq->create($faqEntity));

$this->assertIsInt($this->faq->getSolutionIdFromId($faqEntity->getId(), $faqEntity->getLanguage()));
$this->assertGreaterThan(0, $this->faq->getSolutionIdFromId($faqEntity->getId(), $faqEntity->getLanguage()));
}

public function testHasTranslation(): void
{
$faqEntity = $this->getFaqEntity();
$faqEntity->setId($this->faq->create($faqEntity));

$this->assertTrue($this->faq->hasTranslation($faqEntity->getId(), $faqEntity->getLanguage()));
$this->assertFalse($this->faq->hasTranslation($faqEntity->getId(), 'de'));
}

public function testIsActive(): void
{
$faqEntity = $this->getFaqEntity();
$faqEntity->setId($this->faq->create($faqEntity));

$this->assertTrue($this->faq->isActive($faqEntity->getId(), $faqEntity->getLanguage()));
}

public function testGetRecordBySolutionId(): void
{
$faqEntity = $this->getFaqEntity();
$faqEntity->setSolutionId(42);
$this->faq->create($faqEntity);

$this->faq->getRecordBySolutionId(42);

$this->assertEquals(1, $faqEntity->getId());
}

private function getFaqEntity(): FaqEntity
{
$faqEntity = new FaqEntity();
$faqEntity
->setLanguage('en')
->setActive(true)
->setSticky(true)
->setKeywords('Keywords')
->setQuestion('Question')
->setAnswer('Answer')
->setAuthor('Author')
->setEmail('foo@bar.baz')
->setComment(true)
->setNotes('');

return $faqEntity;
}
}

0 comments on commit 27b4a03

Please sign in to comment.