aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/FormGenerator/tests
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-16 21:09:05 +0200
committermarvin-borner@live.com2018-04-16 21:09:05 +0200
commitcf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch)
tree86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/FormGenerator/tests
parent619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff)
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/FormGenerator/tests')
-rwxr-xr-xmain/app/sprinkles/FormGenerator/tests/Unit/FormGeneratorTest.php408
-rwxr-xr-xmain/app/sprinkles/FormGenerator/tests/Unit/data/bad.json12
-rwxr-xr-xmain/app/sprinkles/FormGenerator/tests/Unit/data/good.json67
3 files changed, 487 insertions, 0 deletions
diff --git a/main/app/sprinkles/FormGenerator/tests/Unit/FormGeneratorTest.php b/main/app/sprinkles/FormGenerator/tests/Unit/FormGeneratorTest.php
new file mode 100755
index 0000000..066e98d
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/tests/Unit/FormGeneratorTest.php
@@ -0,0 +1,408 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Tests\Unit;
+
+use UserFrosting\Tests\TestCase;
+use UserFrosting\Tests\DatabaseTransactions;
+use League\FactoryMuffin\Faker\Facade as Faker;
+use UserFrosting\Support\Repository\Loader\YamlFileLoader;
+use UserFrosting\Fortress\RequestSchema;
+use UserFrosting\Fortress\RequestSchema\RequestSchemaRepository;
+use UserFrosting\Fortress\RequestDataTransformer;
+use UserFrosting\Fortress\ServerSideValidator;
+use UserFrosting\Fortress\Adapter\JqueryValidationAdapter;
+use UserFrosting\Sprinkle\FormGenerator\Form;
+use UserFrosting\Sprinkle\FormGenerator\Element\InputInterface;
+
+
+/**
+ * FormGeneratorTest
+ * The FormGenerator unit tests.
+ */
+class FormGeneratorTest extends TestCase
+{
+ var $basePath;
+
+ public function setUp()
+ {
+ parent::setUp();
+ $this->basePath = __DIR__ . '/data';
+ }
+
+ /**
+ * Test the base `Test` element class works on it's own
+ */
+ public function testTextFormElement()
+ {
+ // Get Schema
+ $loader = new YamlFileLoader($this->basePath . '/good.json');
+ $schema = new RequestSchemaRepository($loader->load());
+
+ // Get TextInput from the `name` element of the schema
+ $inputSchema = $schema["name"]["form"];
+ $textInput = new \UserFrosting\Sprinkle\FormGenerator\Element\Text("name", $inputSchema);
+
+ // Test instanceof $textInput
+ $this->assertInstanceof(InputInterface::class, $textInput);
+
+ // Parse the input
+ $text = $textInput->parse();
+
+ // Test the parsing
+ $expected = [
+ "type" => "text",
+ "label" => "Project Name",
+ "icon" => "fa-flag",
+ "autocomplete" => "off",
+ "class" => "form-control",
+ "placeholder" => "Project Name",
+ 'name' => 'name',
+ 'id' => 'field_name',
+ 'value' => ''
+ ];
+
+ // We test the generated result
+ $this->assertEquals($expected, $text);
+ }
+
+ /**
+ * This test make sure the `Text` element works correctly when a current
+ * value is passed to the constructor. Should return the same as the
+ * previous test, but with the `value` setup instead of empty
+ */
+ public function testTextFormElementWithData()
+ {
+ // Get Schema
+ $loader = new YamlFileLoader($this->basePath . '/good.json');
+ $schema = new RequestSchemaRepository($loader->load());
+
+ // Get TextInput from the `name` element of the schema
+ $inputSchema = $schema["name"]["form"];
+ $textInput = new \UserFrosting\Sprinkle\FormGenerator\Element\Text("name", $inputSchema, "The Bar project");
+
+ // Test instanceof $textInput
+ $this->assertInstanceof(InputInterface::class, $textInput);
+
+ // Parse the input
+ $text = $textInput->parse();
+
+ // Test the parsing
+ $expected = [
+ "type" => "text",
+ "label" => "Project Name",
+ "icon" => "fa-flag",
+ "autocomplete" => "off",
+ "class" => "form-control",
+ "placeholder" => "Project Name",
+ 'name' => 'name',
+ 'id' => 'field_name',
+ 'value' => 'The Bar project'
+ ];
+
+ // We test the generated result
+ $this->assertEquals($expected, $text);
+ }
+
+ /**
+ * This test is the same as the one before, but we test the `owener` field with some data
+ * This make sure the `default` schema field will work correctly when empty data is passed
+ */
+ public function testTextFormElementWithEmptyData()
+ {
+ // Get Schema
+ $loader = new YamlFileLoader($this->basePath . '/good.json');
+ $schema = new RequestSchemaRepository($loader->load());
+
+ // Get TextInput from the `name` element of the schema
+ $inputSchema = $schema["owner"]["form"];
+ $textInput = new \UserFrosting\Sprinkle\FormGenerator\Element\Text("owner", $inputSchema, "");
+
+ // Test instanceof $textInput
+ $this->assertInstanceof(InputInterface::class, $textInput);
+
+ // Parse the input
+ $text = $textInput->parse();
+
+ // Test the parsing
+ $expected = [
+ 'label' => 'Project Owner',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => '', //Shoudn't be a value here ! "" is overwritting "Foo"
+ 'name' => 'owner',
+ 'id' => 'owner',
+ 'type' => 'text',
+ 'icon' => 'fa-user',
+ 'placeholder' => 'Project Owner',
+ 'default' => 'Foo'
+ ];
+
+ // We test the generated result
+ $this->assertEquals($expected, $text);
+ }
+
+ /**
+ * Test the Form Class.
+ * Run the test with no current values (empty form)
+ */
+ public function testForm()
+ {
+ // Get Schema
+ $loader = new YamlFileLoader($this->basePath . '/good.json');
+ $schema = new RequestSchemaRepository($loader->load());
+
+ // Generate the form
+ $form = new Form($schema);
+
+ // Test to make sure the class creation is fine
+ $this->assertInstanceof(Form::class, $form);
+
+ // Test the form generation
+ $generatedForm = $form->generate();
+ $this->assertInternalType("array", $generatedForm);
+
+ // Test one of the form input
+ $expected = [
+ 'number' => [
+ 'label' => 'Project Number',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => '',
+ 'name' => 'number',
+ 'id' => 'field_number',
+ 'type' => 'number',
+ 'icon' => 'fa-edit',
+ 'placeholder' => 'Project Number'
+ ],
+ 'owner' => [
+ 'label' => 'Project Owner',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => 'Foo',
+ 'name' => 'owner',
+ 'id' => 'owner',
+ 'type' => 'text',
+ 'icon' => 'fa-user',
+ 'placeholder' => 'Project Owner',
+ 'default' => 'Foo'
+ ],
+ 'name' => [
+ 'label' => 'Project Name',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => '',
+ 'name' => 'name',
+ 'id' => 'field_name',
+ 'type' => 'text',
+ 'icon' => 'fa-flag',
+ 'placeholder' => 'Project Name'
+ ],
+ 'description' => [
+ 'label' => 'Project Description',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => '',
+ 'name' => 'description',
+ 'rows' => 5,
+ 'id' => 'field_description',
+ 'type' => 'textarea',
+ 'icon' => 'fa-pencil',
+ 'placeholder' => 'Project Description'
+ ],
+ 'status' => [
+ 'label' => 'Project Status',
+ 'class' => 'form-control js-select2',
+ 'value' => '',
+ 'name' => 'status',
+ 'id' => 'field_status',
+ 'type' => 'select',
+ 'options' => [
+ 0 => 'Closed',
+ 1 => 'Open'
+ ]
+ ],
+ 'active' => [
+ 'label' => 'Active',
+ 'class' => 'js-icheck',
+ 'name' => 'active',
+ 'id' => 'field_active',
+ 'type' => 'checkbox',
+ 'binary' => true
+ ],
+ 'hidden' => [
+ 'value' => 'Something',
+ 'name' => 'hidden',
+ 'id' => 'field_hidden',
+ 'type' => 'hidden'
+ ],
+ 'alert' => [
+ 'class' => 'alert-success',
+ 'icon' => 'fa-check',
+ 'value' => 'You\'re awesome!',
+ 'name' => 'alert',
+ 'type' => 'alert'
+ ]
+ ];
+
+ // We test the generated result
+ $this->assertEquals($expected, $generatedForm);
+ }
+
+ /**
+ * Test the Form Clas with values to make sure filled form works correctly
+ */
+ public function testFormWithData()
+ {
+ // Get Schema
+ $loader = new YamlFileLoader($this->basePath . '/good.json');
+ $schema = new RequestSchemaRepository($loader->load());
+
+ // The data
+ $data = [
+ "name" => "Bar project",
+ "owner" => "",
+ "description" => "The bar project is less awesome, but at least it's open.",
+ "status" => 1,
+ "hiddenString" => "The Bar secret code is...",
+ "completion" => 12,
+ "active" => true
+ ];
+
+ // Generate the form
+ $form = new Form($schema, $data);
+
+ // Test to make sure the class creation is fine
+ $this->assertInstanceof(Form::class, $form);
+
+ // Test the form generation
+ $generatedForm = $form->generate();
+ $this->assertInternalType("array", $generatedForm);
+
+ // Test one of the form input
+ $expected = [
+ 'number' => [
+ 'label' => 'Project Number',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => '',
+ 'name' => 'number',
+ 'id' => 'field_number',
+ 'type' => 'number',
+ 'icon' => 'fa-edit',
+ 'placeholder' => 'Project Number'
+ ],
+ 'name' => [
+ 'label' => 'Project Name',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => 'Bar project', //Value here !
+ 'name' => 'name',
+ 'id' => 'field_name',
+ 'type' => 'text',
+ 'icon' => 'fa-flag',
+ 'placeholder' => 'Project Name'
+ ],
+ 'owner' => [
+ 'label' => 'Project Owner',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => '', //Shoudn't be a value here ! "" is overwritting "Foo"
+ 'name' => 'owner',
+ 'id' => 'owner',
+ 'type' => 'text',
+ 'icon' => 'fa-user',
+ 'placeholder' => 'Project Owner',
+ 'default' => 'Foo'
+ ],
+ 'description' => [
+ 'label' => 'Project Description',
+ 'autocomplete' => 'off',
+ 'class' => 'form-control',
+ 'value' => 'The bar project is less awesome, but at least it\'s open.', //Value here !
+ 'name' => 'description',
+ 'rows' => 5,
+ 'id' => 'field_description',
+ 'type' => 'textarea',
+ 'icon' => 'fa-pencil',
+ 'placeholder' => 'Project Description'
+ ],
+ 'status' => [
+ 'label' => 'Project Status',
+ 'class' => 'form-control js-select2',
+ 'value' => 1, //Value here !
+ 'name' => 'status',
+ 'id' => 'field_status',
+ 'type' => 'select',
+ 'options' => [
+ 0 => 'Closed',
+ 1 => 'Open'
+ ]
+ ],
+ 'active' => [
+ 'label' => 'Active',
+ 'class' => 'js-icheck',
+ 'name' => 'active',
+ 'id' => 'field_active',
+ 'type' => 'checkbox',
+ 'checked' => 'checked', //Value here !
+ 'binary' => true
+ ],
+ 'hidden' => [
+ 'value' => 'Something',
+ 'name' => 'hidden',
+ 'id' => 'field_hidden',
+ 'type' => 'hidden'
+ ],
+ 'alert' => [
+ 'class' => 'alert-success',
+ 'icon' => 'fa-check',
+ 'value' => 'You\'re awesome!',
+ 'name' => 'alert',
+ 'type' => 'alert'
+ ]
+ ];
+
+ // We test the generated result
+ $this->assertEquals($expected, $generatedForm);
+ }
+
+ /**
+ * Test a non existant input type. It's supposed to not find the class and
+ * default back to the `Text` element class.
+ */
+ public function testUndefinedFormElement()
+ {
+ // Get Schema
+ $loader = new YamlFileLoader($this->basePath . '/bad.json');
+ $schema = new RequestSchemaRepository($loader->load());
+
+ // Generate the form
+ $form = new Form($schema);
+
+ // Test to make sure the class creation is fine
+ $this->assertInstanceof(Form::class, $form);
+
+ // Test the form generation
+ $generatedForm = $form->generate();
+ $this->assertInternalType("array", $generatedForm);
+
+ // Test one of the form input
+ $expected = [
+ "type" => "foo",
+ "autocomplete" => "off",
+ "class" => "form-control",
+ 'name' => 'myField',
+ 'id' => 'field_myField',
+ 'value' => ''
+ ];
+
+ // We test the generated result
+ $this->assertEquals($expected, $generatedForm['myField']);
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/tests/Unit/data/bad.json b/main/app/sprinkles/FormGenerator/tests/Unit/data/bad.json
new file mode 100755
index 0000000..683add2
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/tests/Unit/data/bad.json
@@ -0,0 +1,12 @@
+{
+ "myField" : {
+ "form" : {
+ "type" : "foo"
+ }
+ },
+ "myOtherField" : {
+ "form" : {
+ "value" : "Bar"
+ }
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/tests/Unit/data/good.json b/main/app/sprinkles/FormGenerator/tests/Unit/data/good.json
new file mode 100755
index 0000000..61d5233
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/tests/Unit/data/good.json
@@ -0,0 +1,67 @@
+{
+ "number" : {
+ "form" : {
+ "type" : "number",
+ "label" : "Project Number",
+ "icon" : "fa-edit",
+ "placeholder" : "Project Number"
+ }
+ },
+ "name" : {
+ "form" : {
+ "type" : "text",
+ "label" : "Project Name",
+ "icon" : "fa-flag",
+ "placeholder" : "Project Name"
+ }
+ },
+ "owner" : {
+ "form" : {
+ "type" : "text",
+ "label" : "Project Owner",
+ "icon" : "fa-user",
+ "id" : "owner",
+ "placeholder" : "Project Owner",
+ "default" : "Foo"
+ }
+ },
+ "description" : {
+ "form" : {
+ "type" : "textarea",
+ "label" : "Project Description",
+ "icon" : "fa-pencil",
+ "placeholder" : "Project Description",
+ "rows" : 5
+ }
+ },
+ "status" : {
+ "form" : {
+ "type" : "select",
+ "label" : "Project Status",
+ "options" : {
+ "0" : "Closed",
+ "1" : "Open"
+ }
+ }
+ },
+ "active" : {
+ "form" : {
+ "type" : "checkbox",
+ "label" : "Active"
+ }
+ },
+ "hidden" : {
+ "form" : {
+ "type" : "hidden",
+ "value" : "Something"
+ }
+ },
+ "alert" : {
+ "form" : {
+ "type" : "alert",
+ "class" : "alert-success",
+ "icon" : "fa-check",
+ "value" : "You're awesome!"
+ }
+ }
+}