From e5aabb09e745f21a607ea7d0ac4bd4c3ce945465 Mon Sep 17 00:00:00 2001 From: alikon Date: Thu, 26 Dec 2024 18:31:59 +0100 Subject: [PATCH 1/3] add tag history endpoint --- .../webservices/tags/src/Extension/Tags.php | 30 ++++++++++ .../api/com_contenthistory/Tag.cy.js | 60 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tests/System/integration/api/com_contenthistory/Tag.cy.js diff --git a/plugins/webservices/tags/src/Extension/Tags.php b/plugins/webservices/tags/src/Extension/Tags.php index 66ef032084a1b..6bf1b0415f7e5 100644 --- a/plugins/webservices/tags/src/Extension/Tags.php +++ b/plugins/webservices/tags/src/Extension/Tags.php @@ -13,6 +13,7 @@ use Joomla\CMS\Event\Application\BeforeApiRouteEvent; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Event\SubscriberInterface; +use Joomla\Router\Route; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -57,5 +58,34 @@ public function onBeforeApiRoute(BeforeApiRouteEvent $event): void 'tags', ['component' => 'com_tags'] ); + + $this->createTagHistoryRoutes($router); + } + + /** + * Create contenthistory routes + * + * @param ApiRouter &$router The API Routing object + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + private function createTagHistoryRoutes(&$router): void + { + $defaults = [ + 'component' => 'com_contenthistory', + 'type_alias' => 'com_tags.tag', + 'type_id' => 8, + ]; + $getDefaults = array_merge(['public' => false], $defaults); + + $routes = [ + new Route(['GET'], 'v1/tags/:id/contenthistory', 'history.displayList', ['id' => '(\d+)'], $getDefaults), + new Route(['PATCH'], 'v1/tags/:id/contenthistory/keep', 'history.keep', ['id' => '(\d+)'], $defaults), + new Route(['DELETE'], 'v1/tags/:id/contenthistory', 'history.delete', ['id' => '(\d+)'], $defaults), + ]; + + $router->addRoutes($routes); } } diff --git a/tests/System/integration/api/com_contenthistory/Tag.cy.js b/tests/System/integration/api/com_contenthistory/Tag.cy.js new file mode 100644 index 0000000000000..4491b4bfd3ee3 --- /dev/null +++ b/tests/System/integration/api/com_contenthistory/Tag.cy.js @@ -0,0 +1,60 @@ +describe('Test that contenthistory for tag API endpoint', () => { + beforeEach(() => { + cy.task('queryDB', "DELETE FROM #__tags where title = 'automated test tag'"); + cy.task('queryDB', 'DELETE FROM #__history'); + }); + + it('can get the history of an existing tag', () => { + cy.api_post('/tags', { + title: 'automated test tag', parent_id: 1, level: 1, description: '', language: '*', + }) + .then((tag) => cy.api_get(`/tags/${tag.body.data.attributes.id}/contenthistory`)) + .then((response) => { + // Assert response status + expect(response.status).to.eq(200); + + // Extract the `data` array + const { data: historyEntries } = response.body; + cy.log(`History Entries: ${historyEntries.length}`); + + // Iterate through each history entry + historyEntries.forEach((entry) => { + const { attributes } = entry; + + // Access top-level attributes + const historyId = entry.id; + const saveDate = attributes.save_date; + const { editor } = attributes; + const characterCount = attributes.character_count; + + // Access nested `version_data` + const versionData = attributes.version_data; + const tagTitle = versionData.title; + const { alias } = versionData; + const createdTime = versionData.created_time; + const modifiedTime = versionData.modified_time; + + // Log details for debugging + cy.log(`History ID: ${historyId}`); + cy.log(`Save Date: ${saveDate}`); + cy.log(`Editor: ${editor}`); + cy.log(`Character Count: ${characterCount}`); + cy.log(`Tag Title: ${tagTitle}`); + cy.log(`Alias: ${alias}`); + cy.log(`Created Time: ${createdTime}`); + cy.log(`Modified Time: ${modifiedTime}`); + + // Perform assertions + expect(attributes).to.have.property('editor_user_id'); + expect(versionData).to.have.property('title'); + expect(versionData).to.have.property('modified_time'); + expect(tagTitle).to.eq('automated test tag'); + }); + + // Check the total pages from metadata + const totalPages = response.body.meta['total-pages']; + expect(totalPages).to.eq(1); + cy.log(`Total Pages: ${totalPages}`); + }); + }); +}); From a4c61cca49b0c5ef0fab349384e8bfea6dc92036 Mon Sep 17 00:00:00 2001 From: alikon Date: Thu, 26 Dec 2024 18:59:10 +0100 Subject: [PATCH 2/3] cs --- tests/System/integration/api/com_contenthistory/Tag.cy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/System/integration/api/com_contenthistory/Tag.cy.js b/tests/System/integration/api/com_contenthistory/Tag.cy.js index 4491b4bfd3ee3..c4655acefbddf 100644 --- a/tests/System/integration/api/com_contenthistory/Tag.cy.js +++ b/tests/System/integration/api/com_contenthistory/Tag.cy.js @@ -8,7 +8,7 @@ describe('Test that contenthistory for tag API endpoint', () => { cy.api_post('/tags', { title: 'automated test tag', parent_id: 1, level: 1, description: '', language: '*', }) - .then((tag) => cy.api_get(`/tags/${tag.body.data.attributes.id}/contenthistory`)) + .then((tag) => cy.api_get(`/tags/${tag.body.data.attributes.id}/contenthistory`)) .then((response) => { // Assert response status expect(response.status).to.eq(200); From e49363deee4f37eb75a0f0c63c64611c924f0e2e Mon Sep 17 00:00:00 2001 From: Nicola Galgano Date: Fri, 27 Dec 2024 12:56:31 +0100 Subject: [PATCH 3/3] description --- plugins/webservices/tags/src/Extension/Tags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/webservices/tags/src/Extension/Tags.php b/plugins/webservices/tags/src/Extension/Tags.php index 6bf1b0415f7e5..b366e33b74eed 100644 --- a/plugins/webservices/tags/src/Extension/Tags.php +++ b/plugins/webservices/tags/src/Extension/Tags.php @@ -63,7 +63,7 @@ public function onBeforeApiRoute(BeforeApiRouteEvent $event): void } /** - * Create contenthistory routes + * Create tag history routes * * @param ApiRouter &$router The API Routing object *