From 7cdded3d8718ade42e03f22e769645ec82cf4bf9 Mon Sep 17 00:00:00 2001 From: alikon Date: Fri, 27 Dec 2024 16:22:20 +0100 Subject: [PATCH] content-category-history-endpoint --- .../content/src/Extension/Content.php | 29 ++++++++++ .../com_contenthistory/Content_Category.cy.js | 58 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tests/System/integration/api/com_contenthistory/Content_Category.cy.js diff --git a/plugins/webservices/content/src/Extension/Content.php b/plugins/webservices/content/src/Extension/Content.php index fcb076421e96a..f8df1ddeb079e 100644 --- a/plugins/webservices/content/src/Extension/Content.php +++ b/plugins/webservices/content/src/Extension/Content.php @@ -69,6 +69,8 @@ public function onBeforeApiRoute(BeforeApiRouteEvent $event): void $this->createFieldsRoutes($router); $this->createContentHistoryRoutes($router); + + $this->createContentCategoryHistoryRoutes($router); } /** @@ -133,4 +135,31 @@ private function createContentHistoryRoutes(&$router): void $router->addRoutes($routes); } + + /** + * Create content category history routes + * + * @param ApiRouter &$router The API Routing object + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + private function createContentCategoryHistoryRoutes(&$router): void + { + $defaults = [ + 'component' => 'com_contenthistory', + 'type_alias' => 'com_content.category', + 'type_id' => 5, + ]; + $getDefaults = array_merge(['public' => false], $defaults); + + $routes = [ + new Route(['GET'], 'v1/content/category/:id/contenthistory', 'history.displayList', ['id' => '(\d+)'], $getDefaults), + new Route(['PATCH'], 'v1/content/category/:id/contenthistory/keep', 'history.keep', ['id' => '(\d+)'], $defaults), + new Route(['DELETE'], 'v1/content/category/:id/contenthistory', 'history.delete', ['id' => '(\d+)'], $defaults), + ]; + + $router->addRoutes($routes); + } } diff --git a/tests/System/integration/api/com_contenthistory/Content_Category.cy.js b/tests/System/integration/api/com_contenthistory/Content_Category.cy.js new file mode 100644 index 0000000000000..3b691e9476fc2 --- /dev/null +++ b/tests/System/integration/api/com_contenthistory/Content_Category.cy.js @@ -0,0 +1,58 @@ +describe('Test that contenthistory for content category API endpoint', () => { + beforeEach(() => { + cy.task('queryDB', "DELETE FROM #__categories WHERE title = 'automated test content category'"); + cy.task('queryDB', 'DELETE FROM #__history'); + }); + + it('can get the history of an existing article category', () => { + cy.api_post('/content/categories', { title: 'automated test content category', description: 'automated test content category description' }) + .then((category) => cy.api_get(`/content/category/${category.body.data.attributes.id}/contenthistory`)) + .then((response) => { + // Assert response status + expect(response.status).to.eq(200); + + // Extract the `data` array + const historyEntries = response.body.data; + 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 categoryTitle = 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(`Category Title: ${categoryTitle}`); + 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(categoryTitle).to.eq('automated test content category'); + }); + + // Check the total pages from metadata + const totalPages = response.body.meta['total-pages']; + expect(totalPages).to.eq(1); + cy.log(`Total Pages: ${totalPages}`); + }); + }); +});