Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor settings form #74

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions src/Form/ConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

donquixote marked this conversation as resolved.
Show resolved Hide resolved
namespace Drupal\collabora_online\Form;

use Drupal\Core\Form\ConfigFormBase;
Expand All @@ -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,
];
Expand All @@ -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,
];
donquixote marked this conversation as resolved.
Show resolved Hide resolved

$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);
Expand All @@ -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'))
Expand Down
128 changes: 128 additions & 0 deletions tests/src/Functional/ConfigFormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

namespace Drupal\Tests\collabora_online\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;

/**
* Tests the Collabora configuration.
*/
class ConfigFormTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'collabora_online',
];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* Tests the configuration for the Collabora settings form.
*/
public function testConfigForm(): void {
$assert_session = $this->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');
}

}
10 changes: 10 additions & 0 deletions tests/src/Kernel/MediaHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

namespace Drupal\Tests\collabora_online\Kernel;
Expand Down
Loading