diff --git a/core/modules/system/src/Form/ThemeSettingsForm.php b/core/modules/system/src/Form/ThemeSettingsForm.php index f7651d2..466e959 100644 --- a/core/modules/system/src/Form/ThemeSettingsForm.php +++ b/core/modules/system/src/Form/ThemeSettingsForm.php @@ -331,7 +331,13 @@ public function buildForm(array $form, FormStateInterface $form_state, $theme = foreach ($filenames as $filename) { if (file_exists($filename)) { require_once $filename; - $form_state->addBuildInfo('files', [$filename]); + + // The file must be require for cached form too. + $files = $form_state->getBuildInfo()['files']; + if (!in_array($filename, $files)) { + $files[] = $filename; + } + $form_state->addBuildInfo('files', $files); } } diff --git a/core/modules/system/src/Tests/Theme/ThemeTest.php b/core/modules/system/src/Tests/Theme/ThemeTest.php index 6df1fec..64f330e 100644 --- a/core/modules/system/src/Tests/Theme/ThemeTest.php +++ b/core/modules/system/src/Tests/Theme/ThemeTest.php @@ -216,7 +216,7 @@ public function testListThemes() { // Check if ThemeHandlerInterface::listInfo() returns disabled themes. // Check for base theme and subtheme lists. $base_theme_list = ['test_basetheme' => 'Theme test base theme']; - $sub_theme_list = ['test_subsubtheme' => 'Theme test subsubtheme', 'test_subtheme' => 'Theme test subtheme', 'test_theme_alter' => 'Theme test alter']; + $sub_theme_list = ['test_subsubtheme' => 'Theme test subsubtheme', 'test_subtheme' => 'Theme test subtheme']; $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, 'Base theme\'s object includes list of subthemes.'); $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.'); diff --git a/core/modules/system/tests/src/Functional/Theme/ThemeTestCustomSettings.php b/core/modules/system/tests/src/Functional/Theme/ThemeTestCustomSettings.php deleted file mode 100644 index 824e908..0000000 --- a/core/modules/system/tests/src/Functional/Theme/ThemeTestCustomSettings.php +++ /dev/null @@ -1,121 +0,0 @@ -bigUser = $this->drupalCreateUser(['administer themes']); - // Install theme. - \Drupal::service('theme_handler')->install(['test_theme', 'test_theme_alter']); - } - - /** - * Retrieves a sample file of the specified type. - * - * @return \Drupal\file\FileInterface - */ - protected function getTestFile($type_name, $size = NULL) { - // Get a file to upload. - $file = current($this->drupalGetTestFiles($type_name, $size)); - - // Add a filesize property to files as would be read by - // \Drupal\file\Entity\File::load(). - $file->filesize = filesize($file->uri); - - return File::create((array) $file); - } - - /** - * Retrieves the fid of the last inserted file. - */ - protected function getLastFileId() { - return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); - } - - /** - * Tests extended theme settings with custom fields and submit handlers. - */ - public function testCustomThemeSettings() { - $themes = ['test_theme', 'test_theme_alter']; - - foreach ($themes as $theme) { - $this->config('system.theme') - ->set('default', $theme) - ->save(); - - $settings_path = 'admin/appearance/settings/' . $theme; - - $this->drupalLogin($this->bigUser); - - $this->drupalGet($settings_path); - $this->assertResponse(200); - - // Add a new managed file. - $image_file = $this->getTestFile('image'); - $image_file_path = drupal_realpath($image_file->getFileUri()); - $edit = [ - 'files[custom_logo]' => $image_file_path, - ]; - // Submit the managed_file form. - $this->drupalPostForm(NULL, $edit, t('Upload')); - // Assert the new file is uploaded as temporary. This file should not be - // saved as permanent if settings are not submited. - $fid1 = $this->getLastFileId(); - $file1 = File::load($fid1); - $this->assertFalse($file1->isPermanent()); - - // Edit more options, attach a new file and submit settings form. - $edit = [ - 'toggle_favicon' => FALSE, - 'custom_flag' => TRUE, - 'custom_text' => 'Tests text', - 'files[custom_logo]' => $image_file_path, - ]; - $this->drupalPostForm($settings_path, $edit, t('Save configuration')); - - $fid2 = $this->getLastFileId(); - $this->assertFieldByName('toggle_favicon', $edit['toggle_favicon']); - $this->assertFieldByName('custom_flag', $edit['custom_flag']); - $this->assertFieldByName('custom_text', $edit['custom_text']); - - // Assert the uploaded file is saved as permanent. - $file2 = File::load($fid2); - $this->assertTrue($file2->isPermanent()); - // Assert the first uploaded file is still temporally. - $this->assertFalse($file1->isPermanent()); - } - - } - -} diff --git a/core/modules/system/tests/src/FunctionalJavascript/ThemeFormSettingsTest.php b/core/modules/system/tests/src/FunctionalJavascript/ThemeFormSettingsTest.php new file mode 100644 index 0000000..8e4729f --- /dev/null +++ b/core/modules/system/tests/src/FunctionalJavascript/ThemeFormSettingsTest.php @@ -0,0 +1,78 @@ +drupalCreateUser(['administer themes']); + $this->drupalLogin($admin); + } + + /** + * Tests that submission handler works correctly. + * + * @dataProvider providerTestFormSettingsSubmissionHandler + */ + public function testFormSettingsSubmissionHandler($theme) { + + \Drupal::service('theme_handler')->install([$theme]); + + $page = $this->getSession()->getPage(); + $assert_session = $this->assertSession(); + + $this->drupalGet("admin/appearance/settings/$theme"); + + // Add a new managed file. + $file = current($this->getTestFiles('image')); + $image_file_path = \Drupal::service('file_system')->realpath($file->uri); + $page->attachFileToField('files[custom_logo]', $image_file_path); + $assert_session->waitForButton('custom_logo_remove_button'); + + // Assert the new file is uploaded as temporary. This file should not be + // saved as permanent if settings are not submited. + $image_field = $this->xpath('//input[@name="custom_logo[fids]"]')[0]; + $file = File::load($image_field->getValue()); + $this->assertFalse($file->isPermanent()); + + $page->pressButton('Save configuration'); + \Drupal::entityTypeManager()->getStorage('file')->resetCache(); + + // Assert the uploaded file is saved as permanent. + $image_field = $this->xpath('//input[@name="custom_logo[fids]"]')[0]; + $file = File::load($image_field->getValue()); + $this->assertTrue($file->isPermanent()); + } + + /** + * Provides test data for ::testFormSettingsSubmissionHandler(). + */ + public function providerTestFormSettingsSubmissionHandler() { + return [ + 'test theme.theme' => ['test_theme_theme'], + 'test theme-settings.php' => ['test_theme_settings'], + ]; + } + +} diff --git a/core/modules/system/tests/themes/test_theme/config/schema/test_theme.schema.yml b/core/modules/system/tests/themes/test_theme/config/schema/test_theme.schema.yml deleted file mode 100644 index 31af721..0000000 --- a/core/modules/system/tests/themes/test_theme/config/schema/test_theme.schema.yml +++ /dev/null @@ -1,18 +0,0 @@ -# Schema for the configuration files of the Test theme. - -test_theme.settings: - type: theme_settings - label: 'Test theme settings' - mapping: - custom_flag: - type: boolean - label: 'Custom flag' - custom_text: - type: string - label: 'Custom text' - custom_logo: - type: sequence - label: 'Custom logo' - sequence: - type: integer - label: 'fids' diff --git a/core/modules/system/tests/themes/test_theme/test_theme.theme b/core/modules/system/tests/themes/test_theme/test_theme.theme index 51ef4b8..aa399b66 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.theme +++ b/core/modules/system/tests/themes/test_theme/test_theme.theme @@ -2,14 +2,6 @@ /** * @file - * Test to ensure theme compatibility with managed files. - */ - -use Drupal\Core\Form\FormStateInterface; -use Drupal\file\Entity\File; - -/** - * @file * Theme to help test the Twig engine. */ @@ -162,51 +154,3 @@ function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__flamin function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose(&$variables) { $variables['bar'] = 'Moose'; } - -/** - * Implements hook_form_system_theme_settings_alter(). - */ -function test_theme_form_system_theme_settings_alter(&$form, FormStateInterface $form_state) { - // Alter default field. - $form['toggle_favicon']['#default_value'] = FALSE; - - // Add custom fields. - $form['custom_flag'] = [ - '#type' => 'checkbox', - '#title' => t('Custom flag.'), - '#default_value' => theme_get_setting('custom_flag'), - ]; - $form['custom_text'] = [ - '#type' => 'textfield', - '#title' => t('Custom textfield.'), - '#default_value' => theme_get_setting('custom_text'), - ]; - $logo_path = 'public://test'; - $success = file_prepare_directory($logo_path, FILE_CREATE_DIRECTORY); - $form['custom_logo'] = [ - '#type' => 'managed_file', - '#name' => 'custom_logo', - '#title' => t('Secondary logo.'), - '#default_value' => theme_get_setting('custom_logo'), - '#progress_indicator' => 'bar', - '#progress_message' => t('Please wait...'), - '#upload_location' => $logo_path, - '#upload_validators' => [ - 'file_validate_extensions' => ['gif png jpg jpeg'], - ], - ]; - // Add a new submission handler. - $form['#submit'][] = 'test_theme_form_system_theme_settings_submit'; -} - -/** - * Test theme form settings submission handler. - */ -function test_theme_form_system_theme_settings_submit(&$form, FormStateInterface $form_state) { - if ($file_id = $form_state->getValue(['custom_logo', '0'])) { - // Make submitted files permanent. - $file = File::load($file_id); - $file->setPermanent(); - $file->save(); - } -} diff --git a/core/modules/system/tests/themes/test_theme_alter/config/schema/test_theme_alter.schema.yml b/core/modules/system/tests/themes/test_theme_alter/config/schema/test_theme_alter.schema.yml deleted file mode 100644 index c8be03b..0000000 --- a/core/modules/system/tests/themes/test_theme_alter/config/schema/test_theme_alter.schema.yml +++ /dev/null @@ -1,18 +0,0 @@ -# Schema for the configuration files of the Test theme alter. - -test_theme_alter.settings: - type: theme_settings - label: 'Test theme alter settings' - mapping: - custom_flag: - type: boolean - label: 'Custom flag' - custom_text: - type: string - label: 'Custom text' - custom_logo: - type: sequence - label: 'Custom logo' - sequence: - type: integer - label: 'fids' diff --git a/core/modules/system/tests/themes/test_theme_alter/test_theme_alter.info.yml b/core/modules/system/tests/themes/test_theme_alter/test_theme_alter.info.yml deleted file mode 100644 index d718e05..0000000 --- a/core/modules/system/tests/themes/test_theme_alter/test_theme_alter.info.yml +++ /dev/null @@ -1,6 +0,0 @@ -name: 'Theme test alter' -type: theme -description: 'Test theme alter that extends theme settings options' -version: VERSION -core: 8.x -base theme: test_basetheme diff --git a/core/modules/system/tests/themes/test_theme_alter/theme-settings.php b/core/modules/system/tests/themes/test_theme_alter/theme-settings.php deleted file mode 100644 index 1ecc1da..0000000 --- a/core/modules/system/tests/themes/test_theme_alter/theme-settings.php +++ /dev/null @@ -1,56 +0,0 @@ - 'checkbox', - '#title' => t('Custom flag.'), - '#default_value' => theme_get_setting('custom_flag'), - ]; - $form['custom_text'] = [ - '#type' => 'textfield', - '#title' => t('Custom textfield.'), - '#default_value' => theme_get_setting('custom_text'), - ]; - $logo_path = 'public://test'; - $success = file_prepare_directory($logo_path, FILE_CREATE_DIRECTORY); - $form['custom_logo'] = [ - '#type' => 'managed_file', - '#title' => t('Secondary logo.'), - '#default_value' => theme_get_setting('custom_logo'), - '#progress_indicator' => 'bar', - '#progress_message' => t('Please wait...'), - '#upload_location' => $logo_path, - '#upload_validators' => [ - 'file_validate_extensions' => ['gif png jpg jpeg'], - ], - ]; - // Add a new submission handler. - $form['#submit'][] = 'test_theme_alter_form_system_theme_settings_submit'; -} - -/** - * Test theme form settings submission handler. - */ -function test_theme_alter_form_system_theme_settings_submit(&$form, FormStateInterface $form_state) { - if ($file_id = $form_state->getValue(['custom_logo', '0'])) { - // Make submitted files permanent. - $file = File::load($file_id); - $file->setPermanent(); - $file->save(); - } -} diff --git a/core/modules/system/tests/themes/test_theme_settings/config/schema/test_theme_settings.schema.yml b/core/modules/system/tests/themes/test_theme_settings/config/schema/test_theme_settings.schema.yml new file mode 100644 index 0000000..a53e94a --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_settings/config/schema/test_theme_settings.schema.yml @@ -0,0 +1,12 @@ +# Schema for the configuration files of the Test theme settings. + +test_theme_settings.settings: + type: theme_settings + label: 'Test theme settings' + mapping: + custom_logo: + type: sequence + label: 'Custom logo' + sequence: + type: integer + label: 'fids' diff --git a/core/modules/system/tests/themes/test_theme_settings/test_theme_settings.info.yml b/core/modules/system/tests/themes/test_theme_settings/test_theme_settings.info.yml new file mode 100644 index 0000000..38dd300 --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_settings/test_theme_settings.info.yml @@ -0,0 +1,6 @@ +name: 'Theme test theme-settings.php' +type: theme +description: 'Test theme that extends theme settings options via theme-settings.php file' +version: VERSION +core: 8.x +base theme: false diff --git a/core/modules/system/tests/themes/test_theme_settings/theme-settings.php b/core/modules/system/tests/themes/test_theme_settings/theme-settings.php new file mode 100644 index 0000000..e82825e --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_settings/theme-settings.php @@ -0,0 +1,35 @@ + 'managed_file', + '#title' => t('Secondary logo.'), + '#default_value' => theme_get_setting('custom_logo'), + '#progress_indicator' => 'bar', + '#progress_message' => t('Please wait...'), + '#upload_location' => 'public://test', + '#upload_validators' => [ + 'file_validate_extensions' => ['gif png jpg jpeg'], + ], + ]; + + // Add a new submission handler. + $form['#submit'][] = 'test_theme_settings_form_system_theme_settings_submit'; +} + +/** + * Test theme form settings submission handler. + */ +function test_theme_settings_form_system_theme_settings_submit(&$form, FormStateInterface $form_state) { + if ($file_id = $form_state->getValue(['custom_logo', '0'])) { + $file = File::load($file_id); + $file->setPermanent(); + $file->save(); + } +} diff --git a/core/modules/system/tests/themes/test_theme_theme/config/schema/test_theme_theme.schema.yml b/core/modules/system/tests/themes/test_theme_theme/config/schema/test_theme_theme.schema.yml new file mode 100644 index 0000000..fcd044e --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_theme/config/schema/test_theme_theme.schema.yml @@ -0,0 +1,12 @@ +# Schema for the configuration files of the Test theme theme. + +test_theme_theme.settings: + type: theme_settings + label: 'Test theme settings' + mapping: + custom_logo: + type: sequence + label: 'Custom logo' + sequence: + type: integer + label: 'fids' diff --git a/core/modules/system/tests/themes/test_theme_theme/test_theme_theme.info.yml b/core/modules/system/tests/themes/test_theme_theme/test_theme_theme.info.yml new file mode 100644 index 0000000..c67a4be --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_theme/test_theme_theme.info.yml @@ -0,0 +1,6 @@ +name: 'Theme test theme.theme file' +type: theme +description: 'Test theme that extends theme settings options via theme.theme file' +version: VERSION +core: 8.x +base theme: false diff --git a/core/modules/system/tests/themes/test_theme_theme/test_theme_theme.theme b/core/modules/system/tests/themes/test_theme_theme/test_theme_theme.theme new file mode 100644 index 0000000..00f214a --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_theme/test_theme_theme.theme @@ -0,0 +1,36 @@ + 'managed_file', + '#title' => t('Secondary logo.'), + '#default_value' => theme_get_setting('custom_logo'), + '#progress_indicator' => 'bar', + '#progress_message' => t('Please wait...'), + '#upload_location' => 'public://test', + '#upload_validators' => [ + 'file_validate_extensions' => ['gif png jpg jpeg'], + ], + ]; + + // Add a new submission handler. + $form['#submit'][] = 'test_theme_theme_form_system_theme_settings_submit'; +} + +/** + * Test theme form settings submission handler. + */ +function test_theme_theme_form_system_theme_settings_submit(&$form, FormStateInterface $form_state) { + if ($file_id = $form_state->getValue(['custom_logo', '0'])) { + $file = File::load($file_id); + $file->setPermanent(); + $file->save(); + } +}