diff --git a/src/Form/ConfigForm.php b/src/Form/ConfigForm.php index 284e54d9..87a32058 100644 --- a/src/Form/ConfigForm.php +++ b/src/Form/ConfigForm.php @@ -10,6 +10,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +declare(strict_types=1); + namespace Drupal\collabora_online\Form; use Drupal\Core\Form\ConfigFormBase; @@ -25,14 +27,14 @@ class ConfigForm extends ConfigFormBase { /** * {@inheritdoc} */ - public function getFormId() { + public function getFormId(): string { return 'collabora_configform'; } /** * {@inheritdoc} */ - public function getEditableConfigNames() { + public function getEditableConfigNames(): array { return [ static::SETTINGS, ]; @@ -41,47 +43,50 @@ public function getEditableConfigNames() { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { + public function buildForm(array $form, FormStateInterface $form_state): array { $config = $this->config(static::SETTINGS); + $cool_settings = $config->get('cool'); $form['server'] = [ - '#type' => 'textfield', + '#type' => 'url', '#title' => $this->t('Collabora Online server URL'), - '#default_value' => $config->get('cool')['server'], + '#default_value' => $cool_settings['server'] ?? '', '#required' => TRUE, ]; $form['wopi_base'] = [ - '#type' => 'textfield', - '#title' => $this->t('WOPI host URL. Likely https://<drupal_server>'), - '#default_value' => $config->get('cool')['wopi_base'], + '#type' => 'url', + '#title' => $this->t('WOPI host URL'), + '#description' => $this->t('Likely https://<drupal_server>'), + '#default_value' => $cool_settings['wopi_base'] ?? '', '#required' => TRUE, ]; $form['key_id'] = [ '#type' => 'textfield', '#title' => $this->t('JWT private key ID'), - '#default_value' => $config->get('cool')['key_id'], + '#default_value' => $cool_settings['key_id'] ?? '', '#required' => TRUE, ]; $form['access_token_ttl'] = [ - '#type' => 'textfield', + '#type' => 'number', '#title' => $this->t('Access Token Expiration (in seconds)'), - '#default_value' => $config->get('cool')['access_token_ttl'], + '#default_value' => $cool_settings['access_token_ttl'] ?? 0, + '#min' => 0, '#required' => TRUE, ]; $form['disable_cert_check'] = [ '#type' => 'checkbox', '#title' => $this->t('Disable TLS certificate check for COOL.'), - '#default_value' => $config->get('cool')['disable_cert_check'], + '#default_value' => $cool_settings['disable_cert_check'] ?? FALSE, ]; $form['allowfullscreen'] = [ '#type' => 'checkbox', '#title' => $this->t('Allow COOL to use fullscreen mode.'), - '#default_value' => $config->get('cool')['allowfullscreen'] ?? FALSE, + '#default_value' => $cool_settings['allowfullscreen'] ?? FALSE, ]; return parent::buildForm($form, $form_state); @@ -90,12 +95,21 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function submitForm(array &$form, FormStateInterface $form_state) { + public function validateForm(array &$form, FormStateInterface $form_state) { + // Remove slashes at the end of wopi_base URL. $wopi_base = rtrim($form_state->getValue('wopi_base'), '/'); + $form_state->setValueForElement($form['wopi_base'], $wopi_base); + parent::validateForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { $this->config(static::SETTINGS) ->set('cool.server', $form_state->getValue('server')) - ->set('cool.wopi_base', $wopi_base) + ->set('cool.wopi_base', $form_state->getValue('wopi_base')) ->set('cool.key_id', $form_state->getValue('key_id')) ->set('cool.access_token_ttl', $form_state->getValue('access_token_ttl')) ->set('cool.disable_cert_check', $form_state->getValue('disable_cert_check')) diff --git a/tests/src/Functional/ConfigFormTest.php b/tests/src/Functional/ConfigFormTest.php new file mode 100644 index 00000000..bbf7329c --- /dev/null +++ b/tests/src/Functional/ConfigFormTest.php @@ -0,0 +1,128 @@ +assertSession(); + + // User without permission can't access the configuration page. + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + $assert_session->pageTextContains('You are not authorized to access this page.'); + $assert_session->statusCodeEquals(403); + + // User with permission can access the configuration page. + $user = $this->createUser(['administer site configuration']); + $this->drupalLogin($user); + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + $assert_session->statusCodeEquals(200); + + // The form contains default values from module install. + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + $assert_session->fieldValueEquals('Collabora Online server URL', 'https://localhost:9980/'); + $assert_session->fieldValueEquals('WOPI host URL', 'https://localhost/'); + $assert_session->fieldValueEquals('JWT private key ID', 'cool'); + $assert_session->fieldValueEquals('Access Token Expiration (in seconds)', '86400'); + $assert_session->fieldValueEquals('Disable TLS certificate check for COOL.', ''); + $assert_session->fieldValueEquals('Allow COOL to use fullscreen mode.', '1'); + + // Change the form values, then submit the form. + $assert_session->fieldExists('Collabora Online server URL') + ->setValue('http://collaboraserver.com/'); + $assert_session->fieldExists('WOPI host URL') + ->setValue('http://wopihost.com/'); + $assert_session->fieldExists('JWT private key ID') + ->setValue('name_of_a_key'); + $assert_session->fieldExists('Access Token Expiration (in seconds)') + ->setValue('3600'); + $assert_session->fieldExists('Disable TLS certificate check for COOL.') + ->check(); + // Since default is checked we disable the full screen option. + $assert_session->fieldExists('Allow COOL to use fullscreen mode.') + ->uncheck(); + $assert_session->buttonExists('Save configuration')->press(); + $assert_session->statusMessageContains('The configuration options have been saved.', 'status'); + + // The settings have been updated. + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + $assert_session->fieldValueEquals('Collabora Online server URL', 'http://collaboraserver.com/'); + // Slash is removed at the end of Wopi URL. + $assert_session->fieldValueEquals('WOPI host URL', 'http://wopihost.com'); + $assert_session->fieldValueEquals('JWT private key ID', 'name_of_a_key'); + $assert_session->fieldValueEquals('Access Token Expiration (in seconds)', '3600'); + $assert_session->fieldValueEquals('Disable TLS certificate check for COOL.', '1'); + $assert_session->fieldValueEquals('Allow COOL to use fullscreen mode.', ''); + + // Test validation of required fields. + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + $assert_session->fieldExists('Collabora Online server URL')->setValue(''); + $assert_session->fieldExists('WOPI host URL')->setValue(''); + $assert_session->fieldExists('JWT private key ID')->setValue(''); + $assert_session->fieldExists('Access Token Expiration (in seconds)')->setValue(''); + $assert_session->fieldExists('Disable TLS certificate check for COOL.')->uncheck(); + $assert_session->fieldExists('Allow COOL to use fullscreen mode.')->uncheck(); + $assert_session->buttonExists('Save configuration')->press(); + $assert_session->statusMessageContains('Collabora Online server URL field is required.', 'error'); + $assert_session->statusMessageContains('WOPI host URL field is required.', 'error'); + $assert_session->statusMessageContains('JWT private key ID field is required.', 'error'); + $assert_session->statusMessageContains('Access Token Expiration (in seconds) field is required.', 'error'); + + // Test validation of bad form values. + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + // Set invalid value for URL fields. + $assert_session->fieldExists('Collabora Online server URL')->setValue('/internal'); + $assert_session->fieldExists('WOPI host URL')->setValue('any-other-value'); + // Set invalid values for numeric field. + $assert_session->fieldExists('Access Token Expiration (in seconds)')->setValue('text'); + $assert_session->buttonExists('Save configuration')->press(); + $assert_session->statusMessageContains('The URL /internal is not valid.', 'error'); + $assert_session->statusMessageContains('The URL any-other-value is not valid.', 'error'); + $assert_session->statusMessageNotContains('Access Token Expiration (in seconds) must be a number.', 'status'); + + // Test form with no configuration. + \Drupal::configFactory()->getEditable('collabora_online.settings')->setData([])->save(); + $this->drupalGet(Url::fromRoute('collabora-online.settings')); + $assert_session->fieldValueEquals('Collabora Online server URL', ''); + $assert_session->fieldValueEquals('WOPI host URL', ''); + $assert_session->fieldValueEquals('JWT private key ID', ''); + $assert_session->fieldValueEquals('Access Token Expiration (in seconds)', '0'); + $assert_session->fieldValueEquals('Disable TLS certificate check for COOL.', ''); + $assert_session->fieldValueEquals('Allow COOL to use fullscreen mode.', ''); + $assert_session->buttonExists('Save configuration'); + } + +} diff --git a/tests/src/Kernel/MediaHelperTest.php b/tests/src/Kernel/MediaHelperTest.php index 8f371ddc..d92d62ac 100644 --- a/tests/src/Kernel/MediaHelperTest.php +++ b/tests/src/Kernel/MediaHelperTest.php @@ -1,5 +1,15 @@