diff --git a/core/modules/locale/tests/modules/locale_test/lib/Drupal/locale_test/LocaleTestBundle.php b/core/modules/locale/tests/modules/locale_test/lib/Drupal/locale_test/LocaleTestBundle.php new file mode 100644 index 0000000..75819f4 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/lib/Drupal/locale_test/LocaleTestBundle.php @@ -0,0 +1,31 @@ +register('locale_test_finish_response_subscriber', 'Drupal\locale_test\LocaleTestFinishResponseSubscriber') + ->addTag('event_subscriber'); + } +} diff --git a/core/modules/locale/tests/modules/locale_test/lib/Drupal/locale_test/LocaleTestFinishResponseSubscriber.php b/core/modules/locale/tests/modules/locale_test/lib/Drupal/locale_test/LocaleTestFinishResponseSubscriber.php new file mode 100644 index 0000000..b9b4a7e --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/lib/Drupal/locale_test/LocaleTestFinishResponseSubscriber.php @@ -0,0 +1,58 @@ +lastModified = $event->getResponse()->headers->get('Last-Modified'); + } + + /** + * Sets the 'Last-Modified' HTTP header to the response. + */ + public function setLastModified(FilterResponseEvent $event) { + if (!empty($this->lastModified)) { + $event->getResponse()->headers->set('Last-Modified', $this->lastModified); + } + } +} diff --git a/core/modules/locale/tests/modules/locale_test/locale_test.info b/core/modules/locale/tests/modules/locale_test/locale_test.info index f17a400..5277751 100644 --- a/core/modules/locale/tests/modules/locale_test/locale_test.info +++ b/core/modules/locale/tests/modules/locale_test/locale_test.info @@ -4,6 +4,7 @@ package = Testing version = 1.2 core = 8.x hidden = TRUE +dependencies[] = locale ; Definitions for interface translations. interface translation project = locale_test diff --git a/core/modules/locale/tests/modules/locale_test/locale_test.module b/core/modules/locale/tests/modules/locale_test/locale_test.module index 8f87ddc..7914db1 100644 --- a/core/modules/locale/tests/modules/locale_test/locale_test.module +++ b/core/modules/locale/tests/modules/locale_test/locale_test.module @@ -5,6 +5,9 @@ * Simulate a custom module with a local po file. */ +define('LOCALE_TEST_TIMESTAMP_NEW', REQUEST_TIME - 100); +define('LOCALE_TEST_TIMESTAMP_OLD', REQUEST_TIME - 300); + /** * Implements hook_system_info_alter(). * @@ -14,7 +17,7 @@ function locale_test_system_info_alter(&$info, $file, $type) { // Only modify the system info if required. // By default the locale_test modules are hidden and have a project specified. - // To test the module detection proces by locale_project_list() the + // To test the module detection process by locale_project_list() the // test modules should mimic a custom module. I.e. be non-hidden. if (state()->get('locale.test_system_info_alter')) { if ($file->name == 'locale_test' || $file->name == 'locale_test_translate') { @@ -25,6 +28,55 @@ function locale_test_system_info_alter(&$info, $file, $type) { } /** + * Copies a translation file to the translations folder. + * + * Mock modules use this method to copy their po files to the translations + * folder. They can also specify a timestamp which will be set for the copied + * file in order to be able to test different scenarios with these translation + * files. + * + * @param string $module + * Name of the module providing the translation file that will be copied by + * this method. Will be used for constructing the path for this translation + * file. + * @param string $translation_file + * Name of the translation file that will be copied. + * @param int $timestamp + * (optional) Timestamp will be set as the creation date for the copy of + * the translation file. By default the timestamp is the current time. + */ +function locale_test_copy_translation_file($module, $translation_file, $timestamp = NULL) { + $translation_file_path = drupal_get_path('module', $module) . '/' . $translation_file; + $translation_directory = variable_get('locale_translate_file_directory', conf_path() . '/files/translations'); + $destination = $translation_directory . '/' . $translation_file; + file_unmanaged_copy($translation_file_path, $destination, FILE_EXISTS_REPLACE); + + if (!empty($timestamp)) { + // Set the timestamp of the newly copied file. + touch($destination, $timestamp); + } +} + +/** + * Serves a translation file as a result of a page callback. + * + * @param string $module + * Name of the module providing the translation file that will be served. + * @param string $translation_file + * Name of the translation file that will be served. + * @param int $timestamp + * Timestamp of date that will be set in the 'Last-Modified' HTTP header. + */ +function locale_test_serve_translation_file($module, $translation_file, $timestamp) { + $translation_file_path = drupal_get_path('module', $module) . '/' . $translation_file; + $headers = array( + 'Content-Type' => 'text/x-po; charset=utf-8', + 'Last-Modified' => gmdate(DATE_RFC1123, $timestamp), + ); + return file_transfer(drupal_realpath($translation_file_path), $headers); +} + +/** * Implements hook_locale_translation_projects_alter(). * * The translation status process by default checks the status of the installed diff --git a/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one-8.x-1.1.de.po b/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one-8.x-1.1.de.po new file mode 100644 index 0000000..adf6f55 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one-8.x-1.1.de.po @@ -0,0 +1,16 @@ +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\n" +"MIME-Version: 1.0\\n" +"Content-Type: text/plain; charset=UTF-8\\n" +"Content-Transfer-Encoding: 8bit\\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\\n" + +msgid "January" +msgstr "Januar_1" + +msgid "February" +msgstr "Februar_1" + +msgid "March" +msgstr "Marz_1" diff --git a/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one.info b/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one.info new file mode 100644 index 0000000..5265c28 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one.info @@ -0,0 +1,19 @@ +name = Locale test contributed module one +project = locale_test_contrib_one +description = Translation test module mocking a contributed module with translation files on remote and local file locations. +package = Testing +version = 8.x-1.1 +core = 8.x +dependencies[] = locale_test + +; This module is hidden on the modules page. However Locale needs visible +; modules in order to recognize translations. Comment out the following line +; when you are doing manual testing, so Locale will find the translations +; provided by this module. +hidden = TRUE + +; The module mimics a remote translation source, but the +; `interface translation server pattern` property can't be defined here, because +; it must be an absolute URI and we don't know the host name of the Drupal +; installation. Please refer to +; locale_test_contrib_one_locale_translation_projects_alter(). diff --git a/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one.install b/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one.install new file mode 100644 index 0000000..15e18fa --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test_contrib_one/locale_test_contrib_one.install @@ -0,0 +1,16 @@ + TRUE)) . LOCALE_TEST_CONTRIB_ONE_REMOTE_TRANSLATION_ENDPOINT; + + // Set the server URL and pattern to where the remote po file for this module + // can be found. + if (isset($projects[LOCALE_TEST_CONTRIB_ONE_MODULE_NAME])) { + $projects[LOCALE_TEST_CONTRIB_ONE_MODULE_NAME]['info']['interface translation server pattern'] = $url . '/%core/%project/%project-%version.%language.po'; + } +} + +/** + * Implements hook_menu(). + * + * Provides a callback for the remote translation file. + */ +function locale_test_contrib_one_menu() { + $items = array(); + $items[LOCALE_TEST_CONTRIB_ONE_REMOTE_TRANSLATION_ENDPOINT . '/' . DRUPAL_CORE_COMPATIBILITY . '/' . LOCALE_TEST_CONTRIB_ONE_PO_FILE] = array( + 'title' => 'Testing: !po_file', + 'title arguments' => array('!po_file' => LOCALE_TEST_CONTRIB_ONE_PO_FILE), + 'page callback' => 'locale_test_contrib_one_remote_translation', + 'access callback' => TRUE, + 'type' => MENU_NORMAL_ITEM, + ); + return $items; +} + +/** + * Page callback: Serves remote translation file. + * + * This is the same translation file as the one used for locale po file testing, + * but we serve its content through this callback. + */ +function locale_test_contrib_one_remote_translation() { + return locale_test_serve_translation_file(LOCALE_TEST_CONTRIB_ONE_MODULE_NAME, LOCALE_TEST_CONTRIB_ONE_PO_FILE, LOCALE_TEST_CONTRIB_ONE_PO_TIMESTAMP_REMOTE); +}