From f46feac0c776a3a7591048e963e6df682aed94f3 Mon Sep 17 00:00:00 2001 From: Nate Lampton Date: Fri, 25 Nov 2022 17:29:06 -0700 Subject: [PATCH] Issue #98: Fix displaying metatag options on views. Port metatag_views tests. --- metatag.admin.inc | 5 +- metatag.api.php | 7 +- metatag.module | 6 +- metatag_views/metatag_views.module | 17 +- metatag_views/metatag_views.tokens.inc | 101 ---- metatag_views/metatag_views.views.inc | 1 - metatag_views/tests/metatag_views.i18n.test | 19 +- metatag_views/tests/metatag_views.test | 18 +- metatag_views/tests/metatag_views.tests.info | 12 + .../config/views.view.metatag_views_test.json | 459 ++++++++++++++++++ .../metatag_views_tests.info | 2 +- .../metatag_views_tests.module | 0 12 files changed, 502 insertions(+), 145 deletions(-) delete mode 100644 metatag_views/metatag_views.tokens.inc create mode 100644 metatag_views/tests/metatag_views.tests.info create mode 100644 metatag_views/tests/metatag_views_tests/config/views.view.metatag_views_test.json rename metatag_views/tests/{ => metatag_views_tests}/metatag_views_tests.info (93%) rename metatag_views/tests/{ => metatag_views_tests}/metatag_views_tests.module (100%) diff --git a/metatag.admin.inc b/metatag.admin.inc index d4a60ad..602c869 100644 --- a/metatag.admin.inc +++ b/metatag.admin.inc @@ -269,9 +269,12 @@ function metatag_config_edit_form($form, &$form_state, $config) { $contexts = explode(':', $config['instance']); $options['context'] = $contexts[0]; + $options['token types'] = array(); if ($contexts[0] != 'global') { $info = entity_get_info($contexts[0]); - $options['token types'] = array($info['token type']); + if ($info) { + $options['token types'][] = $info['token type']; + } // Allow the defined tokens to be modified. backdrop_alter('metatag_token_types', $options); } diff --git a/metatag.api.php b/metatag.api.php index d4f1260..7d607a0 100644 --- a/metatag.api.php +++ b/metatag.api.php @@ -268,7 +268,7 @@ function hook_metatag_presave(&$metatags, $entity_type, $entity_id, $revision_id * Allows modules to alter the defined list of tokens available * for metatag patterns replacements. * - * By default only context (for example: global, node, etc...) + * By default, only context (for example: global, node, etc...) * related tokens are made available to metatag patterns replacements. * This hook allows other modules to extend the default declared tokens. * @@ -282,11 +282,6 @@ function hook_metatag_presave(&$metatags, $entity_type, $entity_id, $revision_id * @see metatag_field_attach_form() */ function hook_metatag_token_types_alter(&$options) { - // Watchout: $options['token types'] might be empty. - if (!isset($options['token types'])) { - $options['token types'] = array(); - } - if ($options['context'] == 'config1') { $options['token types'] += array('token_type1', 'token_type2'); } diff --git a/metatag.module b/metatag.module index 4a6765e..50eee1a 100644 --- a/metatag.module +++ b/metatag.module @@ -2128,11 +2128,11 @@ function metatag_preprocess_page(&$variables) { // Account for multi-valued tags. if (!isset($data['#tag']) && !empty($data[0])) { foreach($data as $delta => $subdata) { - backdrop_add_html_head($subdata, 'metatag_preprocess_page-' . $tag . '-' . $delta); + backdrop_add_html_head($subdata, $tag . '-' . $delta); } } else { - backdrop_add_html_head($data, 'metatag_preprocess_page-' . $tag); + backdrop_add_html_head($data, $tag); } } } @@ -2165,7 +2165,7 @@ function metatag_preprocess_maintenance_page(&$variables) { } // ALl other tags are assumed 'meta' tags. else { - backdrop_add_html_head($data, 'metatag_preprocess_maintenance_page'); + backdrop_add_html_head($data, $tag); } } } diff --git a/metatag_views/metatag_views.module b/metatag_views/metatag_views.module index 0b678cb..4654f0c 100644 --- a/metatag_views/metatag_views.module +++ b/metatag_views/metatag_views.module @@ -16,10 +16,19 @@ function metatag_views_autoload_info() { function metatag_views_views_api() { return array( 'api' => 3, - 'path' => backdrop_get_path('module', 'metatag'), + 'path' => backdrop_get_path('module', 'metatag_views'), ); } +/** + * Implements hook_metatag_token_types_alter(). + */ +function metatag_views_metatag_token_types_alter(&$options) { + if ($options['context'] == 'view') { + $options['token types'][] = 'view'; + } +} + /** * Implements hook_view_preview_info_alter(). */ @@ -64,9 +73,9 @@ function metatag_views_views_preview_info_alter(&$rows, $view) { * Implements hook_preprocess_HOOK(). */ function metatag_views_preprocess_page(&$variables) { - // By default do not add meta tags to admin pages. To enable meta tags on - // admin pages set the 'metatag_tag_admin_pages' variable to TRUE. - if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) { + // By default, do not add meta tags to admin pages. To enable meta tags on + // admin pages set the 'tag_admin_pages' config value to TRUE. + if (path_is_admin(current_path()) && config('metatag.settings')->get('tag_admin_pages') == FALSE) { return; } diff --git a/metatag_views/metatag_views.tokens.inc b/metatag_views/metatag_views.tokens.inc deleted file mode 100644 index 32713e2..0000000 --- a/metatag_views/metatag_views.tokens.inc +++ /dev/null @@ -1,101 +0,0 @@ - t('View'), - 'description' => t('Tokens related to views.'), - 'needs-data' => 'view', - ); - $info['tokens']['view']['name'] = array( - 'name' => t('Name'), - 'description' => t('The human-readable name of the view.'), - ); - $info['tokens']['view']['description'] = array( - 'name' => t('Description'), - 'description' => t('The description of the view.'), - ); - $info['tokens']['view']['machine-name'] = array( - 'name' => t('Machine name'), - 'description' => t('The machine-readable name of the view.'), - ); - $info['tokens']['view']['title'] = array( - 'name' => t('Title'), - 'description' => t('The title of current display of the view.'), - ); - $info['tokens']['view']['url'] = array( - 'name' => t('URL'), - 'description' => t('The URL of the view.'), - 'type' => 'url', - ); - - return $info; -} - -/** - * Implements hook_tokens(). - */ -function metatag_views_tokens($type, $tokens, array $data = array(), array $options = array()) { - if (module_hook('views', 'tokens')) { - return array(); - } - - $url_options = array('absolute' => TRUE); - if (isset($options['language'])) { - $url_options['language'] = $options['language']; - } - $sanitize = !empty($options['sanitize']); - $langcode = isset($options['language']) ? $options['language']->langcode : NULL; - - $replacements = array(); - - if ($type == 'view' && !empty($data['view'])) { - $view = $data['view']; - - foreach ($tokens as $name => $original) { - switch ($name) { - case 'name': - $replacements[$original] = $sanitize ? check_plain($view->human_name) : $view->human_name; - break; - - case 'description': - $replacements[$original] = $sanitize ? check_plain($view->description) : $view->description; - break; - - case 'machine-name': - $replacements[$original] = $view->name; - break; - - case 'title': - $title = $view->get_title(); - $replacements[$original] = $sanitize ? check_plain($title) : $title; - break; - - case 'url': - if ($path = $view->get_url()) { - $replacements[$original] = url($path, $url_options); - } - break; - } - } - - // [view:url:*] nested tokens. This only works if Token module is installed. - if ($url_tokens = token_find_with_prefix($tokens, 'url')) { - if ($path = $view->get_url()) { - $replacements += token_generate('url', $url_tokens, array('path' => $path), $options); - } - } - } - - return $replacements; -} diff --git a/metatag_views/metatag_views.views.inc b/metatag_views/metatag_views.views.inc index d523edc..9a0e75e 100644 --- a/metatag_views/metatag_views.views.inc +++ b/metatag_views/metatag_views.views.inc @@ -14,7 +14,6 @@ function metatag_views_views_plugins() { 'help' => t('Provides meta tags for views.'), 'handler' => 'metatag_views_plugin_display_extender_metatags', 'enabled' => TRUE, - 'path' => backdrop_get_path('module', 'metatag_views'), ); return $plugins; diff --git a/metatag_views/tests/metatag_views.i18n.test b/metatag_views/tests/metatag_views.i18n.test index 5d0c164..eaa9038 100644 --- a/metatag_views/tests/metatag_views.i18n.test +++ b/metatag_views/tests/metatag_views.i18n.test @@ -5,17 +5,6 @@ */ class MetatagViewsI18nTest extends MetatagTestHelper { - /** - * {@inheritdoc} - */ - public static function getInfo() { - return array( - 'name' => 'Metatag:Views i18n tests', - 'description' => 'Test Metatag integration via the Metatag:Views module.', - 'group' => 'Metatag', - ); - } - /** * {@inheritdoc} */ @@ -51,11 +40,11 @@ class MetatagViewsI18nTest extends MetatagTestHelper { } /** - * Test the Metatag:Views translations. + * Test the Metatag: Views translations. */ public function testExportedPage() { // Plan out the different translation string tests. - $string_en = 'Testing Metatag:Views.'; + $string_en = 'Testing Metatag: Views.'; $string_fr = 'French page description.'; $config_name = 'metatag_views:metatag_views_test__page:description'; $path = 'metatag-views-test'; @@ -72,7 +61,7 @@ class MetatagViewsI18nTest extends MetatagTestHelper { $this->saveTranslationString($lid, $config_name, 'fr', $string_en, $string_fr); // Load the English page again. - $this->drupalGet($path); + $this->backdropGet($path); $this->assertResponse(200, 'Loaded the default test page again.'); // Confirm the page's description is what we set it to. @@ -82,7 +71,7 @@ class MetatagViewsI18nTest extends MetatagTestHelper { $this->assertNotEqual($xpath[0]['content'], $string_fr); // Load the French page. - $this->drupalGet('fr/' . $path); + $this->backdropGet('fr/' . $path); $this->assertResponse(200, 'Loaded the French test page.'); // Confirm the generator string was translated. diff --git a/metatag_views/tests/metatag_views.test b/metatag_views/tests/metatag_views.test index cde421c..78dc617 100644 --- a/metatag_views/tests/metatag_views.test +++ b/metatag_views/tests/metatag_views.test @@ -5,17 +5,6 @@ */ class MetatagViewsTest extends MetatagTestHelper { - /** - * {@inheritdoc} - */ - public static function getInfo() { - return array( - 'name' => 'Metatag:Views tests', - 'description' => 'Test Metatag integration via the Metatag:Views module.', - 'group' => 'Metatag', - ); - } - /** * {@inheritdoc} */ @@ -26,13 +15,16 @@ class MetatagViewsTest extends MetatagTestHelper { $modules[] = 'metatag_views_tests'; parent::setUp($modules); + + // Enable keywords metatag for testing. + config_set('metatag.settings', 'enabled_tags.keywords', 'keywords'); } /** * Test the Metatag:Views translations on an exported page. */ public function testExportedPage() { - $this->drupalGet('metatag-views-test'); + $this->backdropGet('metatag-views-test'); $this->assertResponse(200); // Test the page title. @@ -41,7 +33,7 @@ class MetatagViewsTest extends MetatagTestHelper { // Test the description meta tag. $xpath = $this->xpath("//meta[@name='description']"); $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.'); - $this->assertEqual($xpath[0]['content'], 'Testing Metatag:Views.'); + $this->assertEqual($xpath[0]['content'], 'Testing Metatag: Views.'); // Test the keywords meta tag. $xpath = $this->xpath("//meta[@name='keywords']"); diff --git a/metatag_views/tests/metatag_views.tests.info b/metatag_views/tests/metatag_views.tests.info new file mode 100644 index 0000000..18e10f6 --- /dev/null +++ b/metatag_views/tests/metatag_views.tests.info @@ -0,0 +1,12 @@ +[MetatagViewsTest] +name = Metatag: Views tests +description = Test Metatag integration via the Metatag: Views module +group = Metatag +file = metatag_views.test + +# @todo: Finish porting i18n tests to Backdrop. +# [MetatagViewsI18nTest] +# name = Metatag: Views i18n tests +# description = Test Metatag i18n integration via the Metatag: Views module. +# group = Metatag +# file = metatag_views.i18n.test diff --git a/metatag_views/tests/metatag_views_tests/config/views.view.metatag_views_test.json b/metatag_views/tests/metatag_views_tests/config/views.view.metatag_views_test.json new file mode 100644 index 0000000..d1bd810 --- /dev/null +++ b/metatag_views/tests/metatag_views_tests/config/views.view.metatag_views_test.json @@ -0,0 +1,459 @@ +{ + "_config_name": "views.view.metatag_views_test", + "name": "metatag_views_test", + "description": "", + "tag": "default", + "disabled": false, + "base_table": "node", + "human_name": "Metatag Views test", + "core": 7, + "display": { + "default": { + "display_title": "Master", + "display_plugin": "default", + "display_options": { + "query": { + "type": "views_query", + "options": [] + }, + "title": "Metatag Views test", + "use_more_always": false, + "access": { + "type": "perm" + }, + "cache": { + "type": "none" + }, + "exposed_form": { + "type": "basic" + }, + "pager": { + "type": "full", + "options": { + "items_per_page": "5" + } + }, + "style_plugin": "default", + "row_plugin": "node", + "row_options": { + "links": false + }, + "fields": { + "title": { + "id": "title", + "table": "node", + "field": "title", + "label": "", + "alter": { + "word_boundary": false, + "ellipsis": false + } + } + }, + "sorts": { + "created": { + "id": "created", + "table": "node", + "field": "created", + "order": "DESC" + } + }, + "filters": { + "status": { + "id": "status", + "table": "node", + "field": "status", + "value": 1, + "group": 1, + "expose": { + "operator": false + } + } + } + } + }, + "page": { + "display_title": "Page", + "display_plugin": "page", + "display_options": { + "query": { + "type": "views_query", + "options": [] + }, + "metatags": { + "und": { + "title": { + "value": "Test Views page title", + "default": "[view:title] | [current-page:pager][site:name]" + }, + "description": { + "value": "Testing Metatag: Views.", + "default": "[view:description]" + }, + "abstract": { + "value": "" + }, + "keywords": { + "value": "Test, page, keywords" + }, + "robots": { + "value": { + "index": 0, + "follow": 0, + "noindex": 0, + "nofollow": 0, + "noarchive": 0, + "nosnippet": 0, + "noodp": 0, + "noydir": 0, + "noimageindex": 0, + "notranslate": 0 + } + }, + "news_keywords": { + "value": "" + }, + "standout": { + "value": "" + }, + "rating": { + "value": "" + }, + "referrer": { + "value": "" + }, + "rights": { + "value": "" + }, + "image_src": { + "value": "" + }, + "canonical": { + "value": "[view:url]", + "default": "[view:url]" + }, + "shortlink": { + "value": "[current-page:url:unaliased]", + "default": "[current-page:url:unaliased]" + }, + "publisher": { + "value": "" + }, + "author": { + "value": "" + }, + "original-source": { + "value": "" + }, + "prev": { + "value": "" + }, + "next": { + "value": "" + }, + "revisit-after": { + "value": "", + "period": "" + }, + "content-language": { + "value": "" + }, + "geo.position": { + "value": "" + }, + "geo.placename": { + "value": "" + }, + "geo.region": { + "value": "" + }, + "icbm": { + "value": "" + }, + "refresh": { + "value": "" + }, + "itemtype": { + "value": "" + }, + "itemprop:name": { + "value": "[current-page:title]", + "default": "[current-page:title]" + }, + "itemprop:description": { + "value": "" + }, + "itemprop:image": { + "value": "" + }, + "theme-color": { + "value": "" + }, + "MobileOptimized": { + "value": "" + }, + "HandheldFriendly": { + "value": "" + }, + "viewport": { + "value": "" + }, + "cleartype": { + "value": "" + }, + "apple-itunes-app": { + "value": "" + }, + "apple-mobile-web-app-capable": { + "value": "" + }, + "apple-mobile-web-app-status-bar-style": { + "value": "" + }, + "format-detection": { + "value": "" + }, + "ios-app-link-alternative": { + "value": "" + }, + "android-app-link-alternative": { + "value": "" + }, + "android-manifest": { + "value": "" + }, + "x-ua-compatible": { + "value": "" + }, + "application-name": { + "value": "" + }, + "msapplication-allowDomainApiCalls": { + "value": "" + }, + "msapplication-allowDomainMetaTags": { + "value": "" + }, + "msapplication-badge": { + "value": "" + }, + "msapplication-config": { + "value": "" + }, + "msapplication-navbutton-color": { + "value": "" + }, + "msapplication-notification": { + "value": "" + }, + "msapplication-square150x150logo": { + "value": "" + }, + "msapplication-square310x310logo": { + "value": "" + }, + "msapplication-square70x70logo": { + "value": "" + }, + "msapplication-wide310x150logo": { + "value": "" + }, + "msapplication-starturl": { + "value": "" + }, + "msapplication-task": { + "value": "" + }, + "msapplication-task-separator": { + "value": "" + }, + "msapplication-tilecolor": { + "value": "" + }, + "msapplication-tileimage": { + "value": "" + }, + "msapplication-tooltip": { + "value": "" + }, + "msapplication-window": { + "value": "" + }, + "og:type": { + "value": "article", + "default": "article" + }, + "og:url": { + "value": "[current-page:url:absolute]", + "default": "[current-page:url:absolute]" + }, + "og:title": { + "value": "[current-page:title]", + "default": "[current-page:title]" + }, + "og:determiner": { + "value": "" + }, + "og:description": { + "value": "" + }, + "og:updated_time": { + "value": "" + }, + "og:see_also": { + "value": "" + }, + "og:image": { + "value": "" + }, + "og:image:url": { + "value": "" + }, + "og:image:secure_url": { + "value": "" + }, + "og:image:type": { + "value": "" + }, + "og:image:width": { + "value": "" + }, + "og:image:height": { + "value": "" + }, + "og:latitude": { + "value": "" + }, + "og:longitude": { + "value": "" + }, + "og:street_address": { + "value": "" + }, + "og:locality": { + "value": "" + }, + "og:region": { + "value": "" + }, + "og:postal_code": { + "value": "" + }, + "og:country_name": { + "value": "" + }, + "og:email": { + "value": "" + }, + "og:phone_number": { + "value": "" + }, + "og:fax_number": { + "value": "" + }, + "og:locale": { + "value": "" + }, + "og:locale:alternate": { + "value": "" + }, + "article:author": { + "value": "" + }, + "article:publisher": { + "value": "" + }, + "article:section": { + "value": "" + }, + "article:tag": { + "value": "" + }, + "article:published_time": { + "value": "" + }, + "article:modified_time": { + "value": "" + }, + "article:expiration_time": { + "value": "" + }, + "profile:first_name": { + "value": "" + }, + "profile:last_name": { + "value": "" + }, + "profile:username": { + "value": "" + }, + "profile:gender": { + "value": "" + }, + "og:audio": { + "value": "" + }, + "og:audio:secure_url": { + "value": "" + }, + "og:audio:type": { + "value": "" + }, + "book:author": { + "value": "" + }, + "book:isbn": { + "value": "" + }, + "book:release_date": { + "value": "" + }, + "book:tag": { + "value": "" + }, + "og:video:url": { + "value": "" + }, + "og:video:secure_url": { + "value": "" + }, + "og:video:width": { + "value": "" + }, + "og:video:height": { + "value": "" + }, + "og:video:type": { + "value": "" + }, + "video:actor": { + "value": "" + }, + "video:actor:role": { + "value": "" + }, + "video:director": { + "value": "" + }, + "video:writer": { + "value": "" + }, + "video:duration": { + "value": "" + }, + "video:release_date": { + "value": "" + }, + "video:tag": { + "value": "" + }, + "video:series": { + "value": "" + } + } + }, + "path": "metatag-views-test" + } + } + } +} diff --git a/metatag_views/tests/metatag_views_tests.info b/metatag_views/tests/metatag_views_tests/metatag_views_tests.info similarity index 93% rename from metatag_views/tests/metatag_views_tests.info rename to metatag_views/tests/metatag_views_tests/metatag_views_tests.info index 252b8ea..ecaecc7 100644 --- a/metatag_views/tests/metatag_views_tests.info +++ b/metatag_views/tests/metatag_views_tests/metatag_views_tests.info @@ -1,6 +1,6 @@ name = Metatag:Views Tests description = Helper module for testing metatag_views.module. -core = 7.x +backdrop = 1.x ; Don't show this on the modules admin page. hidden = TRUE diff --git a/metatag_views/tests/metatag_views_tests.module b/metatag_views/tests/metatag_views_tests/metatag_views_tests.module similarity index 100% rename from metatag_views/tests/metatag_views_tests.module rename to metatag_views/tests/metatag_views_tests/metatag_views_tests.module