Skip to content

Commit

Permalink
Fixed error in JSON example and improved setting with JSON validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathrin84 committed Oct 8, 2020
1 parent 8ce8ab9 commit addc0c0
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ moodle-atto_styles
Changes
-------

### Unreleased

* 2020-10-06 - Fixed error in JSON example and improved setting with JSON validation.
PLEASE NOTE: Now, surrounding square brackets are mandatory when defining multiple styles.
An upgrade script will handle existent settings. However, after updating the plugin
please double check the setting.

### v3.8-r1

* 2020-02-12 - Prepare compatibility for Moodle 3.8.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ There, simply follow the instructions and configure the widget according to your
Here's the configuration which corresponds to the CSS example given above:

```
{
[{
"title": "Red box (solid)",
"type": "block",
"classes": "attostylesbox attostylesbox-solid attostylesbox-solid-red",
Expand Down Expand Up @@ -245,7 +245,7 @@ Here's the configuration which corresponds to the CSS example given above:
"type": "inline",
"classes": "attostylestextmarker attostylestextmarker-pink",
"preview": true
}
}]
```


Expand Down
61 changes: 61 additions & 0 deletions classes/admin_setting_configtextarea_validatejson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Atto styles - Special settings.
*
* @package atto_styles
* @copyright 2020 Kathrin Osswald, Ulm University <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace atto_styles;

defined('MOODLE_INTERNAL') || die();

/**
* Special setting for configtextarea with JSON validation.
*
* @package atto_styles
* @copyright 2020 Kathrin Osswald, Ulm University <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_configtextarea_validatejson extends \admin_setting_configtextarea {

/**
* Validate the contents of the textarea to ensure it's valid JSON.
*
* @param string $data The JSON from text field.
* @return mixed bool true for success or string:error on failure.
*/
public function validate($data) {
// If no entry is made validate true, otherwise the setting would always return an error and
// could not be saved.
if (empty($data)) {
return true;
}

$jsonobject = json_decode($data);
// If the JSON decoding was not possible (returns null) or encountered errors (returns false)
// then return an error message.
if (!$jsonobject) {
return get_string('jsondecodemessage', 'atto_styles');
}

// Return the result of the parent class' check of the data.
return parent::validate($data);
}
}
65 changes: 65 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Atto styles - Upgrade Script.
*
* @package atto_styles
* @copyright 2020 Kathrin Osswald, Ulm University <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

/**
* Function to upgrade atto_styles
* @param int $oldversion the version we are upgrading from
* @return bool result
*/
function xmldb_atto_styles_upgrade($oldversion) {

if ($oldversion < 2020100200) {
/* JSON validation has been added to the setting and now every entry must be valid JSON.
Up to this point entries without [] brackets were accepted, so we need to add them now
to the existent setting entries. */

$oldconfig = get_config('atto_styles', 'config');

/* Check if rules with multiple JSON heads exist like this
{
"title": "Hero unit box",
"type": "block",
"classes": "hero-unit"
},{
"title": "Muted Well",
"type": "block",
"classes": "well text-muted"
}
and add surrounding square brackets around it to provide valid JSON code in the setting. */
// First character (without leading whitespaces) in string should not be a '['.
// Last character (without trailing whitespaces) in string should not be a ‘]‘.
if (strpos($oldconfig, '},') !== false &&
(trim($oldconfig)[0] != '[') &&
(trim($oldconfig)[-1] != ']')) {
$newconfig = '['.trim($oldconfig).']';
set_config('config', $newconfig, 'atto_styles');
}

upgrade_plugin_savepoint(true, 2020100200, 'atto', 'styles');
}

return true;
}
19 changes: 17 additions & 2 deletions lang/en/atto_styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@
$string['code_example'] = '{<br />
&nbsp;&nbsp;&nbsp;&nbsp;"title": "Blue box",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"type": "block",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"classes": "box blue"<br />
&nbsp;&nbsp;&nbsp;&nbsp;"classes": "box blue",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"preview": true<br />
}';
$string['code_example_bootstrap'] = '{<br />
&nbsp;&nbsp;&nbsp;&nbsp;"title": "Success alert",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"type": "block",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"classes": "alert alert-success"<br />
}';
$string['code_example_bootstrap_multiple'] = '[{<br />
&nbsp;&nbsp;&nbsp;&nbsp;"title": "Success alert",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"type": "block",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"classes": "alert alert-success"<br />
},{<br />
&nbsp;&nbsp;&nbsp;&nbsp;"title": "Danger alert",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"type": "block",<br />
&nbsp;&nbsp;&nbsp;&nbsp;"classes": "alert alert-danger"<br />
}]';
$string['config'] = 'Styles configuration';
$string['config_desc'] = 'Configuration for the styles widget for Atto in JSON format. <br /> You can find an extensive example in the README.md in the section <a href="https://github.com/moodleuulm/moodle-atto_styles/blob/master/README.md#usage--settings">"Usage & Settings"</a>.
<hr />
Expand Down Expand Up @@ -71,8 +80,14 @@
<li><a href="https://getbootstrap.com/docs/4.3/components/badge/">Bootstrap badges</a></li>
<li><a href="https://getbootstrap.com/docs/4.3/components/alerts/">Bootstrap alerts</a></li>
<li><a href="https://getbootstrap.com/docs/4.3/utilities/">Bootstrap utilities</a></li>
</ul>';
</ul>
<hr />
Please note, that multiple definitions need to be surrounded by square brackets.
<br /><br />
For example:<br />
{$a->code_example_bootstrap_multiple}';
$string['inlinehint'] = 'Select some text first to apply this style';
$string['jsondecodemessage'] = 'Entered JSON code is not valid.';
$string['nostyle'] = 'No style';
$string['pluginname'] = 'Styles';
$string['privacy:metadata'] = 'The atto_styles plugin does not store any personal data.';
Expand Down
10 changes: 9 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ function atto_styles_params_for_js($elementid, $options, $fpoptions) {
// Format a string with the active filter set.
// If it is modified - we assume that some sort of text filter is working in this context.
$styles = get_config('atto_styles', 'config');
$styles = '['.$styles.']';

$styles = json_decode($styles);

// If there is a single definition, no square brackets are needed for valid JSON.
// Nevertheless, we need a multidimensional array with the JSON object to proceed.
// So we create one here for this case.
if (!is_array($styles)) {
$new = array();
$new[] = $styles;
$styles = $new;
}

foreach ($styles as $key => $style) {
$styles[$key]->title = format_text($styles[$key]->title);
$styles[$key]->title = strip_tags($styles[$key]->title);
Expand Down
6 changes: 2 additions & 4 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@
$a = new stdClass();
$a->code_example = new lang_string('code_example', 'atto_styles');
$a->code_example_bootstrap = new lang_string('code_example_bootstrap', 'atto_styles');
$a->code_example_bootstrap_multiple = new lang_string('code_example_bootstrap_multiple', 'atto_styles');
$name = new lang_string('config', 'atto_styles');
$desc = new lang_string('config_desc', 'atto_styles', $a);
$default = '';

$setting = new admin_setting_configtextarea('atto_styles/config',
$name,
$desc,
$default);
$setting = new \atto_styles\admin_setting_configtextarea_validatejson('atto_styles/config', $name, $desc, $default);
$settings->add($setting);
}
49 changes: 47 additions & 2 deletions tests/behat/atto_styles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Atto styles
Given I log in as "admin"
And the config value "config" of "atto_styles" is set as admin to multiline
"""
{
[{
"title": "Hero unit box",
"type": "block",
"classes": "hero-unit"
Expand All @@ -22,7 +22,7 @@ Feature: Atto styles
"title": "Warning text",
"type": "inline",
"classes": "label label-warning"
}
}]
"""
And the config value "toolbar" of "editor_atto" is set as admin to multiline
"""
Expand All @@ -41,6 +41,51 @@ Feature: Atto styles
"""
And I log out

@javascript
Scenario: Test JSON validation of the setting
Given I log in as "admin"
And I navigate to "Plugins > Text editors > Atto HTML editor > Styles settings " in site administration
And I set the field "Styles configuration" to multiline:
"""
Just a non JSON string.
"""
And I press "Save changes"
Then I should see "Some settings were not changed due to an error."
And I should see "Entered JSON code is not valid."
When I set the field "Styles configuration" to multiline:
"""
{
"title": "Hero unit box",
"type": "block",
"classes": "hero-unit"
}
"""
And I press "Save changes"
Then I should not see "Some settings were not changed due to an error."
And I should not see "Entered JSON code is not valid."
When I set the field "Styles configuration" to multiline:
"""
[{
"title": "Hero unit box",
"type": "block"
"classes": "hero-unit"
}]
"""
And I press "Save changes"
Then I should see "Some settings were not changed due to an error."
And I should see "Entered JSON code is not valid."
When I set the field "Styles configuration" to multiline:
"""
[{
"title": "Hero unit box",
"type": "block",
"classes": "hero-unit"
}]
"""
And I press "Save changes"
Then I should not see "Some settings were not changed due to an error."
And I should not see "Entered JSON code is not valid."

@javascript
Scenario Outline: Test inline styles with one and two classes
Given I log in as "admin"
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'atto_styles';
$plugin->version = 2020031100;
$plugin->version = 2020100200;
$plugin->release = 'v3.8-r1';
$plugin->requires = 2019111800;
$plugin->maturity = MATURITY_STABLE;

0 comments on commit addc0c0

Please sign in to comment.