Ejemplo n.º 1
0
 /**
  * Test subforms get unique suffixes.
  */
 public function testUniqueSuffixesForSubforms()
 {
     $initializer = new Initializer();
     $fieldBearer1 = WritableBearerBuilder::begin()->addWritable(new Field([], [], 'literal', 'A literal field', [], "field"))->addWritable(new Field([], [], 'literal', 'A second literal field', []), "field2")->build();
     $fieldBearer2 = WritableBearerBuilder::begin()->addWritable(new Field([], [], 'literal', 'A literal field', [], "field"))->addWritable(new Field([], [], 'literal', 'A second literal field', []), "field2")->build();
     $form1 = FormBuilder::begin()->addWritableBearer($fieldBearer1)->setId("f-" . (string) rand())->build();
     $form2 = FormBuilder::begin()->addWritableBearer($fieldBearer2)->setId("f-" . (string) rand())->build();
     $form = FormBuilder::begin()->setId("f-" . (string) rand())->addWritable($form1, 'Form1')->addWritable($form2, 'Form2')->build();
     // Initialize the form to add suffixes
     $_SERVER["REQUEST_METHOD"] = "GET";
     $initializer->visitForm($form);
     // Grab all of the fields from both of the sub forms
     $writables = array_merge(array_values($form->getWritableBearer()->getWritableByHandle("Form1")->getWritableBearer()->getWritables()), array_values($form->getWritableBearer()->getWritableByHandle("Form2")->getWritableBearer()->getWritables()));
     // Map each field to a serialization of its suffixes
     $suffixes = [];
     foreach ($writables as $writable) {
         if ($writable instanceof FieldInterface) {
             $suffixes[] = serialize($writable->getSuffixes());
         }
     }
     // Assert that every set of suffixes is unique
     $this->assertEquals(sizeof($suffixes), sizeof(array_unique($suffixes)));
 }
Ejemplo n.º 2
0
 public function testSetFieldAttributes()
 {
     $choices = [ChoiceBuilder::begin()->setValue('choice value')->build()];
     $form = FormBuilder::begin()->setId("f-" . (string) rand())->addWritable(new Field([], [], Field::TYPE_TEXT, 'A text field', []), "field")->setFieldType("field", Field::TYPE_CHOICE)->setFieldChoices("field", $choices)->setInitialFieldValue("field", "new initial field value")->setFieldLabel("field", "new field label")->build();
     /** @var FieldInterface $field */
     $field = $form->getWritableBearer()->getWritableByHandle("field");
     $this->assertEquals(Field::TYPE_CHOICE, $field->getType());
     $this->assertEquals(array_values($choices), array_values($field->getChoices()));
     $this->assertEquals('new initial field value', $field->getInitial());
     $this->assertEquals('new field label', $field->getLabel());
 }
Ejemplo n.º 3
0
 /**
  * @return WritableInterface
  * @throws \Exception If object id not provided, or object not found.
  */
 protected function makeDetail()
 {
     /** @var boolean $idWasProvided */
     $idWasProvided = static::getObjectId() !== null;
     /** @var ObjectWrapperInterface $object */
     $object = $this->getObjectOr404(true);
     /** @var string $className */
     $className = $this->getQuery()->getTitleCasedObjectName();
     /** @var string $tableName */
     $tableName = StringUtils::slugify($className);
     /** @var FormActionInterface $submitAction */
     $submitAction = FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_JAVASCRIPT)->setLabel('Save')->addClass('save')->setTarget("\n                athens.ajax.submitForm(\$(this).closest('form')[0], function(){\n                        athens.multi_panel.closePanel(1);\n                        athens.ajax_section.loadSection('object-manager-table-{$tableName}');\n                    });\n                return false;\n            ")->build();
     /** @var string $deleteUrl */
     $deleteUrl = static::makeUrl([static::MODE_FIELD => static::MODE_DELETE, static::QUERY_INDEX_FIELD => $this->getQueryIndex(), static::OBJECT_ID_FIELD => $object->getPrimaryKey()]);
     /** @var FormActionInterface $deleteAction */
     $deleteAction = FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_JAVASCRIPT)->setLabel('Delete')->setTarget("\n                if (confirm('Are you sure you want to delete this object?')) {\n                    athens.alert.makeAlert('Deleting object.', 'info');\n                    athens.ajax.post(\n                        '{$deleteUrl}',\n                        {},\n                        function() {},\n                        function() {\n                            athens.alert.makeAlert('Object deleted', 'success');\n                            athens.multi_panel.closePanel(1);\n                            athens.ajax_section.loadSection('object-manager-table-{$tableName}');\n                        }\n                    );\n                }\n                return false;\n            ")->build();
     $formBuilder = FormBuilder::begin()->setId('object-manager-detail-form')->addObject($object)->removeWritable($object->getPascalCasedObjectName() . '.Id')->addAction($submitAction);
     if ($idWasProvided === true) {
         $formBuilder->addAction($deleteAction);
     }
     $sectionBuilder = SectionBuilder::begin()->setId('object-manager-detail-form-wrapper')->addLabel($className)->addWritable($formBuilder->build());
     return $sectionBuilder->build();
 }
Ejemplo n.º 4
0
 public function testRenderFormErrors()
 {
     $writer = new HTMLWriter();
     $_SERVER["REQUEST_URI"] = "";
     /* Field not required, no data provided: no field errors */
     $field = new Field([], [], "text", "An unrequired field", "5", false, [], 200);
     $form = FormBuilder::begin()->setId("f-" . (string) rand())->addWritable($field)->build();
     // Confirm that the form is valid and has no errors
     $this->assertTrue($form->isValid());
     $this->assertEmpty($form->getErrors());
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitForm($form));
     // Assert that the result does not display any errors
     $this->assertNotContains("form-errors", $result);
     /* Field required, but no data provided: field errors */
     $field = new Field([], [], "text", "A required field", "5", true, [], 200);
     $form = FormBuilder::begin()->setId("f-" . (string) rand())->addWritable($field)->build();
     // Confirm that the form is not valid and does have errors
     $this->assertFalse($form->isValid());
     $this->assertNotEmpty($form->getErrors());
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitForm($form));
     // Assert that the result does display errors
     $this->assertContains("form-errors", $result);
     // Assert that form has been given the class has-errors
     $this->assertContains("class= prevent-double-submit has-errors", $result);
 }