Skip to content

Commit

Permalink
refactor: renamed entity class, added missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 24, 2024
1 parent c234952 commit 9cbc003
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/admin/record.add.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@

// Insert the tags
if ($tags !== '') {
$tagging->saveTags($recordId, explode(',', trim((string) $tags)));
$tagging->create($recordId, explode(',', trim((string) $tags)));
}

// Add user permissions
Expand Down
4 changes: 2 additions & 2 deletions phpmyfaq/admin/record.save.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@

// Insert the tags
if ($tags != '') {
$tagging->saveTags($recordId, explode(',', trim((string) $tags)));
$tagging->create($recordId, explode(',', trim((string) $tags)));
} else {
$tagging->deleteTagsFromRecordId($recordId);
$tagging->deleteByRecordId($recordId);
}

// Add user permissions
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/admin/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

if ('delete-tag' === $action) {
$tagId = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($tags->deleteTag($tagId)) {
if ($tags->delete($tagId)) {
$deleteSuccess = true;
} else {
$deleteSuccess = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use phpMyFAQ\Configuration;
use phpMyFAQ\Controller\AbstractController;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Entity\TagEntity;
use phpMyFAQ\Entity\Tag;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Session\Token;
Expand Down Expand Up @@ -55,11 +55,11 @@ public function update(Request $request): JsonResponse
$id = Filter::filterVar($postData->id, FILTER_VALIDATE_INT);
$newTag = Filter::filterVar($postData->tag, FILTER_SANITIZE_SPECIAL_CHARS);

$tagEntity = new TagEntity();
$tagEntity = new Tag();
$tagEntity->setId($id);
$tagEntity->setName($newTag);

if ($tags->updateTag($tagEntity)) {
if ($tags->update($tagEntity)) {
return $this->json(['updated' => Translation::get('ad_entryins_suc')], Response::HTTP_OK);
} else {
return $this->json(['error' => Translation::get('ad_entryins_fail')], Response::HTTP_BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @package phpMyFAQ\Entity
*/
class TagEntity
class Tag
{
private ?int $id = null;

Expand All @@ -33,7 +33,7 @@ public function getId(): int
return $this->id;
}

public function setId(int $id): TagEntity
public function setId(int $id): Tag
{
$this->id = $id;
return $this;
Expand All @@ -44,7 +44,7 @@ public function getName(): string
return $this->name;
}

public function setName(string $name): TagEntity
public function setName(string $name): Tag
{
$this->name = $name;
return $this;
Expand Down
26 changes: 11 additions & 15 deletions phpmyfaq/src/phpMyFAQ/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace phpMyFAQ;

use phpMyFAQ\Entity\TagEntity as EntityTags;
use phpMyFAQ\Entity\Tag as Tag;

/**
* Class Tags
Expand Down Expand Up @@ -111,14 +111,14 @@ public function getAllTagsById(int $recordId): array
* @param int $recordId Record ID
* @param array<int, string> $tags Array of tags
*/
public function saveTags(int $recordId, array $tags): bool
public function create(int $recordId, array $tags): bool
{
$currentTags = $this->getAllTags();
$registeredTags = [];

// Delete all tag references for the faq record
if ($tags !== []) {
$this->deleteTagsFromRecordId($recordId);
$this->deleteByRecordId($recordId);
}

// Store tags and references for the faq record
Expand Down Expand Up @@ -245,38 +245,36 @@ public function getAllTags(
*
* @param int $recordId Record ID
*/
public function deleteTagsFromRecordId(int $recordId): bool
public function deleteByRecordId(int $recordId): bool
{
$query = sprintf(
'DELETE FROM %sfaqdata_tags WHERE record_id = %d',
Database::getTablePrefix(),
$recordId
);

$this->configuration->getDb()->query($query);

return true;
return (bool) $this->configuration->getDb()->query($query);
}

/**
* Updates a tag.
*/
public function updateTag(EntityTags $entityTags): bool
public function update(Tag $tag): bool
{
$query = sprintf(
"UPDATE %sfaqtags SET tagging_name = '%s' WHERE tagging_id = %d",
Database::getTablePrefix(),
$entityTags->getName(),
$entityTags->getId()
$tag->getName(),
$tag->getId()
);

return $this->configuration->getDb()->query($query);
return (bool) $this->configuration->getDb()->query($query);
}

/**
* Deletes a given tag.
*/
public function deleteTag(int $tagId): bool
public function delete(int $tagId): bool
{
$query = sprintf(
'DELETE FROM %sfaqtags WHERE tagging_id = %d',
Expand All @@ -292,9 +290,7 @@ public function deleteTag(int $tagId): bool
$tagId
);

$this->configuration->getDb()->query($query);

return true;
return (bool) $this->configuration->getDb()->query($query);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions tests/phpMyFAQ/TagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace phpMyFAQ;

use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Entity\Tag;
use PHPUnit\Framework\TestCase;

class TagsTest extends TestCase
{
private Tags $tags;

protected function setUp(): void
{
parent::setUp();

$dbHandle = new Sqlite3();
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$configuration = new Configuration($dbHandle);

$this->tags = new Tags($configuration);
}

protected function tearDown(): void
{
$this->tags->deleteByRecordId(1);
}

public function testCreate(): void
{
$testData = ['Foo', 'Bar', 'Baz'];
$result = $this->tags->create(1, $testData);

$this->assertTrue($result);
}

public function testDeleteByRecordId(): void
{
$testData = ['Foo', 'Bar', 'Baz'];
$this->tags->create(1, $testData);

$this->assertTrue($this->tags->deleteByRecordId(1));
}

public function testUpdate(): void
{
$testData = ['Foo', 'Bar', 'Baz'];
$this->tags->create(1, $testData);

$tag = new Tag();
$tag->setId(1);
$tag->setName('Foooooooo');

$this->assertTrue($this->tags->update($tag));
$this->assertEquals('Foooooooo', $this->tags->getTagNameById(1));
}

public function testDelete(): void
{
$testData = ['Foo', 'Bar', 'Baz'];
$this->tags->create(1, $testData);

$this->assertTrue($this->tags->delete(1));
$this->assertEmpty($this->tags->getTagNameById(1));
}
}

0 comments on commit 9cbc003

Please sign in to comment.