Skip to content

Commit

Permalink
Merge branch 'master' into feature-replacement-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Olewniczak committed Jul 2, 2019
2 parents 3c63c4d + 22adff3 commit f1cfffa
Show file tree
Hide file tree
Showing 40 changed files with 1,469 additions and 605 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
@@ -1,8 +1,12 @@
language: php
php:
- "7.3"
- "7.2"
- "7.1"
- "7.0"
- "5.6"
before_install: wget https://raw.github.com/splitbrain/dokuwiki-travis/master/travis.sh
install: sh travis.sh
script: cd _test && phpunit --stderr --group plugin_bureaucracy
script:
- cd _test
- ./phpunit.phar --group plugin_bureaucracy
46 changes: 29 additions & 17 deletions _test/BureaucracyTest.php
@@ -1,45 +1,57 @@
<?php

namespace dokuwiki\plugin\bureaucracy\test;


class BureaucracyTest extends \DokuWikiTest {
protected $pluginsEnabled = array('bureaucracy');
class BureaucracyTest extends \DokuWikiTest
{

const FORM_PREFIX_HTML = '<form class="bureaucracy__plugin" id="bureaucracy__plugin1" enctype="multipart/form-data" method="post" action="" accept-charset="utf-8"><div class="no">
<input type="hidden" name="sectok" value="" /><input type="hidden" name="bureaucracy[$$id]" value="1" /><fieldset ><legend></legend>';
const FORM_SUFFIX_HTML = '</fieldset>
</div></form>';

protected $pluginsEnabled = ['bureaucracy'];

/**
* Simulate sending of bureaucracy form
*
* @param string $form_syntax syntax to build a bureaucracy form
* @param string $template_syntax syntax used as a page template for the "action template"
* @param array& $validation_errors field labels that were invalid
* @param string|array ...$values values passed to form handler
* @param string|array $form_syntax syntax to build a bureaucracy form
* @param string $template_syntax syntax used as a page template for the "action template"
* @param array & $validation_errors field labels that were invalid
* @param string|array ...$values values passed to form handler
*
* @return string content of newly created page
*/
protected function send_form_action_template($form_syntax, $template_syntax, &$validation_errors, ...$values) {
if (is_array($values[0])) {
$values = $values[0];
}
$id = uniqid('page');
$template_id = uniqid('template');
protected function send_form_action_template($form_syntax, $template_syntax, &$validation_errors, ...$values)
{
$id = uniqid('page', true);
$template_id = uniqid('template', true);

//create full form syntax
if (is_array($form_syntax)) $form_syntax = implode("\n", $form_syntax);
if (is_array($form_syntax)) {
$form_syntax = implode("\n", $form_syntax);
}
$form_syntax = "<form>\naction template $template_id $id\n$form_syntax\n</form>";

saveWikiText($template_id, $template_syntax, 'summary');

/** @var \syntax_plugin_bureaucracy $syntax_plugin */
$syntax_plugin = plugin_load('syntax', 'bureaucracy');
$data = $syntax_plugin->handle($form_syntax, 0, 0, new \Doku_Handler());

$actionData = $data['actions'][0];
/** @var \helper_plugin_bureaucracy_action $action */
$action = plugin_load('helper', $actionData['actionname']);
//this is the only form
$form_id = 0;

for ($i = 0; $i < count($data['fields']); ++$i) {
//set null for not existing values
if (!isset($values[$i])) $values[$i] = null;
/** @var \helper_plugin_bureaucracy_field $field */
foreach ($data['fields'] as $i => $field) {
if (!isset($values[$i])) {
$values[$i] = null;
}

$field = $data['fields'][$i];
$isValid = $field->handle_post($values[$i], $data['fields'], $i, $form_id);
if (!$isValid) {
$validation_errors[] = $field->getParam('label');
Expand Down
97 changes: 97 additions & 0 deletions _test/field_date.test.php
@@ -0,0 +1,97 @@
<?php

namespace dokuwiki\plugin\bureaucracy\test;

use \Doku_Form;

/**
* @group plugin_bureaucracy
* @group plugins
*/
class bureaucracy_field_date_test extends BureaucracyTest
{

public function dataProvider()
{
return [
[
'Date:@@dateLabel@@',
'date "dateLabel"',
'2018-05-15',
'Date:2018-05-15',
[],
'valid date',
],
[
'Date:@@dateLabel@@',
'date "dateLabel"',
'2018.05.15',
null,
['dateLabel'],
'invalid date',
],
[
'Date: @DATE(@@dateLabel@@)@',
'date "dateLabel"',
'2018-02-15',
'Date: 2018/02/15 00:00',
[],
'formatted date with $conf[\'dformat\'] format',
],
[
'Month: @DATE(@@dateLabel@@,%%m)@',
'date "dateLabel"',
'2018-02-15',
'Month: 02',
[],
'formatted date with custom format',
],
];
}

/**
* @dataProvider dataProvider
*
* @param string $templateSyntax
* @param string $formSyntax
* @param string $postedValue
* @param string $expectedWikiText
* @param string $expectedValidationErrors
* @param string $msg
*
*/
public function test_field_date_submit(
$templateSyntax,
$formSyntax,
$postedValue,
$expectedWikiText,
$expectedValidationErrors,
$msg
) {
$actualValidationErrors = [];

$actualWikiText = parent::send_form_action_template(
$formSyntax,
$templateSyntax,
$actualValidationErrors,
$postedValue
);

if (empty($expectedValidationErrors)) {
$this->assertEquals($expectedWikiText, $actualWikiText, $msg);
}
$this->assertEquals($expectedValidationErrors, $actualValidationErrors, $msg);
}

public function test_field_date_render()
{
$formSyntax = 'date "dateLabel"';
$instr = p_get_instructions("<form>\n$formSyntax\n</form>");

$actualHTML = p_render('xhtml', $instr, $info);

$expectedFieldHTML = '<label><span>dateLabel <sup>*</sup></span> <input type="text" name="bureaucracy[0]" class="datepicker edit required" maxlength="10" required="required" /></label>';
$expectedHTML = self::FORM_PREFIX_HTML . "\n$expectedFieldHTML\n" . self::FORM_SUFFIX_HTML;
$this->assertEquals(trim($expectedHTML), trim($actualHTML));
}
}
92 changes: 92 additions & 0 deletions _test/field_email.test.php
@@ -0,0 +1,92 @@
<?php

namespace dokuwiki\plugin\bureaucracy\test;

use \Doku_Form;

/**
* @group plugin_bureaucracy
* @group plugins
*/
class bureaucracy_field_email_test extends BureaucracyTest
{

public function dataProvider()
{
return [
[
'Mail: @@emailLabel@@',
'valid@example.com',
'Mail: valid@example.com',
[],
'valid email',
],
[
'Mail: @@emailLabel@@',
'@MAIL@',
'Mail: @MAIL@',
[],
'@MAIL@ placeholder for user\'s email adress',
],
[
'Mail: @@emailLabel@@',
'invalid@example',
'Mail: invalid@example',
[],
'local email addresses are allowed',
],
[
'Mail: @@emailLabel@@',
'invalid[at]example.com',
null,
['emailLabel'],
'invalid email',
],
];
}

/**
* @dataProvider dataProvider
*
* @param string $templateSyntax
* @param $postedValue
* @param string $expectedWikiText
* @param string $expectedValidationErrors
* @param string $msg
*
*/
public function test_field_email_submit(
$templateSyntax,
$postedValue,
$expectedWikiText,
$expectedValidationErrors,
$msg
) {
$actualValidationErrors = [];

$label = 'emailLabel';
$actualWikiText = parent::send_form_action_template(
"email \"$label\"",
$templateSyntax,
$actualValidationErrors,
$postedValue
);

if (empty($expectedValidationErrors)) {
$this->assertEquals($expectedWikiText, $actualWikiText, $msg);
}
$this->assertEquals($expectedValidationErrors, $actualValidationErrors, $msg);
}

public function test_field_email_render()
{
$formSyntax = 'email emailLabel';
$instr = p_get_instructions("<form>\n$formSyntax\n</form>");

$actualHTML = p_render('xhtml', $instr, $info);

$expectedFieldHTML = '<label><span>emailLabel <sup>*</sup></span> <input type="text" name="bureaucracy[0]" class="edit required" required="required" /></label>';
$expectedHTML = self::FORM_PREFIX_HTML . "\n$expectedFieldHTML\n" . self::FORM_SUFFIX_HTML;
$this->assertEquals(trim($expectedHTML), trim($actualHTML));
}
}
File renamed without changes.
77 changes: 77 additions & 0 deletions _test/field_hidden.test.php
@@ -0,0 +1,77 @@
<?php

namespace dokuwiki\plugin\bureaucracy\test;

use \Doku_Form;

/**
* @group plugin_bureaucracy
* @group plugins
*/
class bureaucracy_field_hidden_test extends BureaucracyTest
{

public function dataProvider()
{
return [
[
'hidden:@@hiddenLabel@@',
'default value of the hidden field',
'default value of the hidden field',
'hidden:default value of the hidden field',
[],
'valid hidden',
],
];
}

/**
* @dataProvider dataProvider
*
* @param string $templateSyntax
* @param $postedValue
* @param string $expectedWikiText
* @param string $expectedValidationErrors
* @param string $msg
*
*/
public function test_field_hidden_submit(
$templateSyntax,
$defaultValue,
$postedValue,
$expectedWikiText,
$expectedValidationErrors,
$msg
) {
$actualValidationErrors = [];

$label = 'hiddenLabel';
$actualWikiText = parent::send_form_action_template(
"hidden \"$label\" \"=$defaultValue\"",
$templateSyntax,
$actualValidationErrors,
$postedValue
);

if (empty($expectedValidationErrors)) {
$this->assertEquals($expectedWikiText, $actualWikiText, $msg);
}
$this->assertEquals($expectedValidationErrors, $actualValidationErrors, $msg);
}

public function test_field_time_render()
{
$formSyntax = 'hidden hiddenLabel "=default value of the hidden field"';
$instr = p_get_instructions("<form>\n$formSyntax\n</form>");

$actualHTML = p_render('xhtml', $instr, $info);

$hiddenFormPrefix = '<form class="bureaucracy__plugin" id="bureaucracy__plugin1" enctype="multipart/form-data" method="post" action="" accept-charset="utf-8"><div class="no">
<input type="hidden" name="sectok" value="" /><input type="hidden" name="bureaucracy[$$id]" value="1" />';
$expectedFieldHTML = '<input type="hidden" name="bureaucracy[0]" value="default value of the hidden field" />';
$hiddenFormSuffix = '</div></form>';
$expectedHTML = "$hiddenFormPrefix$expectedFieldHTML$hiddenFormSuffix";

$this->assertEquals(trim($expectedHTML), trim($actualHTML));
}
}

0 comments on commit f1cfffa

Please sign in to comment.