From 3cc03713ae7904a8f7749205ef455f9dcdd72225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 28 Feb 2024 15:37:49 +0100 Subject: [PATCH] MOBILE-4498 tags: Use advanced features to check if tags are enabled --- .github/workflows/acceptance.yml | 1 + .../block/blogtags/services/block-handler.ts | 12 +++++++ .../block/tags/services/block-handler.ts | 8 +++++ src/core/classes/delegate.ts | 17 +++++++++- .../tag/services/handlers/mainmenu.ts | 9 +----- .../tag/services/tag-area-delegate.ts | 8 +++++ src/core/features/tag/services/tag.ts | 3 +- .../tag/tests/behat/navigation.feature | 31 +++++++++++++++++++ 8 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 src/core/features/tag/tests/behat/navigation.feature diff --git a/.github/workflows/acceptance.yml b/.github/workflows/acceptance.yml index b0135037c..7c40a9c0c 100644 --- a/.github/workflows/acceptance.yml +++ b/.github/workflows/acceptance.yml @@ -93,6 +93,7 @@ jobs: "@core_search" "@core_settings" "@core_siteplugins" + "@core_tag" "@core_user" "@core_usertour" ) diff --git a/src/addons/block/blogtags/services/block-handler.ts b/src/addons/block/blogtags/services/block-handler.ts index 1ace929ac..66ba21369 100644 --- a/src/addons/block/blogtags/services/block-handler.ts +++ b/src/addons/block/blogtags/services/block-handler.ts @@ -18,6 +18,8 @@ import { CoreBlockHandlerData } from '@features/block/services/block-delegate'; import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler'; import { AddonBlockBlogTagsComponent } from '../components/blogtags/blogtags'; import { makeSingleton } from '@singletons'; +import { CoreTag } from '@features/tag/services/tag'; +import { AddonBlog } from '@addons/blog/services/blog'; /** * Block handler. @@ -28,6 +30,16 @@ export class AddonBlockBlogTagsHandlerService extends CoreBlockBaseHandler { name = 'AddonBlockBlogTags'; blockName = 'blog_tags'; + /** + * @inheritdoc + */ + async isEnabled(): Promise { + const tags = await CoreTag.areTagsAvailable(); + const blogs = await AddonBlog.isPluginEnabled(); + + return blogs && tags; + } + /** * Returns the data needed to render the block. * diff --git a/src/addons/block/tags/services/block-handler.ts b/src/addons/block/tags/services/block-handler.ts index 723e16f43..6abfb7690 100644 --- a/src/addons/block/tags/services/block-handler.ts +++ b/src/addons/block/tags/services/block-handler.ts @@ -18,6 +18,7 @@ import { CoreBlockHandlerData } from '@features/block/services/block-delegate'; import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler'; import { AddonBlockTagsComponent } from '../components/tags/tags'; import { makeSingleton } from '@singletons'; +import { CoreTag } from '@features/tag/services/tag'; /** * Block handler. @@ -28,6 +29,13 @@ export class AddonBlockTagsHandlerService extends CoreBlockBaseHandler { name = 'AddonBlockTags'; blockName = 'tags'; + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return await CoreTag.areTagsAvailable(); + } + /** * Returns the data needed to render the block. * diff --git a/src/core/classes/delegate.ts b/src/core/classes/delegate.ts index 2dd5141c9..ba2b73b0f 100644 --- a/src/core/classes/delegate.ts +++ b/src/core/classes/delegate.ts @@ -111,6 +111,15 @@ export class CoreDelegate { } } + /** + * Check if the delegate is enabled so handlers are not updated if not.. + * + * @returns Whether the delegate is enabled. + */ + async isEnabled(): Promise { + return true; + } + /** * Execute a certain function in a enabled handler. * If the handler isn't found or function isn't defined, call the same function in the default handler. @@ -315,6 +324,12 @@ export class CoreDelegate { * @returns Resolved when done. */ async updateHandlers(): Promise { + const enabled = await this.isEnabled(); + + if (!enabled) { + return; + } + const promises: Promise[] = []; const now = Date.now(); @@ -329,7 +344,7 @@ export class CoreDelegate { try { await Promise.all(promises); - } catch (e) { + } catch { // Never reject } diff --git a/src/core/features/tag/services/handlers/mainmenu.ts b/src/core/features/tag/services/handlers/mainmenu.ts index cd88b5740..72d9e9145 100644 --- a/src/core/features/tag/services/handlers/mainmenu.ts +++ b/src/core/features/tag/services/handlers/mainmenu.ts @@ -15,7 +15,6 @@ import { Injectable } from '@angular/core'; import { CoreTag } from '../tag'; import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '@features/mainmenu/services/mainmenu-delegate'; -import { CoreUtils } from '@services/utils/utils'; import { makeSingleton } from '@singletons'; /** @@ -35,13 +34,7 @@ export class CoreTagMainMenuHandlerService implements CoreMainMenuHandler { * @returns Whether or not the handler is enabled on a site level. */ async isEnabled(): Promise { - const available = await CoreTag.areTagsAvailable(); - if (!available) { - return false; - } - - // The only way to check whether tags are enabled on web is to perform a WS call. - return CoreUtils.promiseWorks(CoreTag.getTagCollections()); + return await CoreTag.areTagsAvailable(); } /** diff --git a/src/core/features/tag/services/tag-area-delegate.ts b/src/core/features/tag/services/tag-area-delegate.ts index 5864ea471..64462d5bb 100644 --- a/src/core/features/tag/services/tag-area-delegate.ts +++ b/src/core/features/tag/services/tag-area-delegate.ts @@ -15,6 +15,7 @@ import { Injectable, Type } from '@angular/core'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; import { makeSingleton } from '@singletons'; +import { CoreTag } from './tag'; /** * Interface that all tag area handlers must implement. @@ -53,6 +54,13 @@ export class CoreTagAreaDelegateService extends CoreDelegate super('CoreTagAreaDelegate', true); } + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return CoreTag.areTagsAvailable(); + } + /** * Returns the display name string for this area. * diff --git a/src/core/features/tag/services/tag.ts b/src/core/features/tag/services/tag.ts index cc5261426..ccbc08d6a 100644 --- a/src/core/features/tag/services/tag.ts +++ b/src/core/features/tag/services/tag.ts @@ -53,7 +53,8 @@ export class CoreTagProvider { areTagsAvailableInSite(site?: CoreSite): boolean { site = site || CoreSites.getCurrentSite(); - return !!site && site.wsAvailable('core_tag_get_tagindex_per_area') && + return !!site && site.canUseAdvancedFeature('usetags') && + site.wsAvailable('core_tag_get_tagindex_per_area') && site.wsAvailable('core_tag_get_tag_cloud') && site.wsAvailable('core_tag_get_tag_collections') && !site.isFeatureDisabled('NoDelegate_CoreTag'); diff --git a/src/core/features/tag/tests/behat/navigation.feature b/src/core/features/tag/tests/behat/navigation.feature new file mode 100644 index 000000000..0740d8be4 --- /dev/null +++ b/src/core/features/tag/tests/behat/navigation.feature @@ -0,0 +1,31 @@ +@core_tag @app @javascript +Feature: Tag navigation + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | interests | + | user1 | User | 1 | user1@example.com | Cat | + | user2 | User | 2 | user1@example.com | Cat, Dog | + | user3 | User | 3 | user1@example.com | Dog | + And the following "courses" exist: + | fullname | shortname | tags | + | Course 1 | c1 | Cat, Dog | + | Course 2 | c2 | Cat | + | Course 3 | c3 | Cat | + | Course 4 | c4 | Cat | + | Course 5 | c5 | Cat | + | Course 6 | c6 | Cat | + | Course 7 | c7 | Cat | + + Scenario: Tag menu item is found in the more menu when completion is enabled + Given I entered the app as "user1" + When I press the more menu button in the app + And I press "Tags" in the app + Then I should find "Cat" in the app + Then I should find "Dog" in the app + + Given the following config values are set as admin: + | usetags | 0 | + And I entered the app as "user1" + When I press the more menu button in the app + Then I should not find "Tags" in the app