From 0b3cbdeae507b4a7bbeeecd13f9e36c903717827 Mon Sep 17 00:00:00 2001 From: nathangray Date: Thu, 7 Sep 2017 14:30:38 -0600 Subject: [PATCH] Etemplate - some basic selectbox tests --- api/src/Etemplate/Widget/Select.php | 4 + api/src/Etemplate/Widget/test/SelectTest.php | 246 +++++++++++++++++++ api/templates/test/select_test.xet | 10 + 3 files changed, 260 insertions(+) create mode 100644 api/src/Etemplate/Widget/test/SelectTest.php create mode 100644 api/templates/test/select_test.xet diff --git a/api/src/Etemplate/Widget/Select.php b/api/src/Etemplate/Widget/Select.php index 4400db7675..fe962fb64a 100644 --- a/api/src/Etemplate/Widget/Select.php +++ b/api/src/Etemplate/Widget/Select.php @@ -198,6 +198,10 @@ class Select extends Etemplate\Widget { self::set_validation_error($form_name,lang('Field must not be empty !!!',$value),''); } + if (!$this->attrs['multiple'] && is_array($value) && count($value) > 1) + { + $value = array_shift($value); + } // some widgets sub-types need some post-processing // ToDo: move it together with preprocessing to clientside switch ($widget_type) diff --git a/api/src/Etemplate/Widget/test/SelectTest.php b/api/src/Etemplate/Widget/test/SelectTest.php new file mode 100644 index 0000000000..076f0f1700 --- /dev/null +++ b/api/src/Etemplate/Widget/test/SelectTest.php @@ -0,0 +1,246 @@ + 'α Alpha', + 'Β'=> 'β Beta', + 'Γ'=> 'γ Gamma', + 'Δ'=> 'δ Delta', + 'Ε'=> 'ε Epsilon', + 'Ζ'=> 'ζ Zeta', + 'Η'=> 'η Eta', + 'Θ'=> 'θ Theta', + 'Ι'=> 'ι Iota', + 'Κ'=> 'κ Kappa', + 'Λ'=> 'λ Lambda', + 'Μ'=> 'μ Mu', + 'Ν'=> 'ν Nu', + 'Ξ'=> 'ξ Xi', + 'Ο'=> 'ο Omicron', + 'Π'=> 'π Pi', + 'Ρ'=> 'ρ Rho', + 'Σ'=> 'σ Sigma', + 'Τ'=> 'τ Tau', + 'Υ'=> 'υ Upsilon', + 'Φ'=> 'φ Phi', + 'Χ'=> 'χ Chi', + 'Ψ'=> 'ψ Psi', + 'Ω'=> 'ω Omega', + ); + + /** + * Test the widget's basic functionality - we put data in, it comes back + * unchanged. + * + * @dataProvider validProvider + */ + public function testBasic($content) + { + // Instanciate the template + $etemplate = new Etemplate(); + $etemplate->read(static::TEST_TEMPLATE, 'test'); + + // Send it around + $result = $this->mockedRoundTrip($etemplate, array('widget' => $content), array('widget' => self::VALUES)); + + // Test it + $this->validateTest($result, array('widget' => $content)); + } + + /** + * These are all valid + * + */ + public function validProvider() + { + $values = array(array('')); + foreach(self::VALUES as $key => $label) + { + $values[] = array($key); + } + return $values; + } + + + /** + * Check validation with failing values + * + * @param string $content + * + * @dataProvider validationProvider + */ + public function testValidation($content) + { + // Instanciate the template + $etemplate = new Etemplate(); + $etemplate->read(static::TEST_TEMPLATE, 'test'); + + // Send it around + $result = $this->mockedRoundTrip($etemplate, array('widget' => $content), array('widget' => self::VALUES)); + + // Test it + $this->validateTest($result, array(), array('widget'=> true)); + } + + /** + * These are all invalid + */ + public function validationProvider() + { + // All these are invalid, and should not give a value back + return array( + array('0'), + array('Alpha'), + array('A'), // This is ASCII A, not Alpha + array('Α,Β'), + ); + } + + /** + * Test to make sure a selectbox that accepts multiple actually does + * + * @param Array $content + * @param Array $expected + * + * @dataProvider multipleProvider + */ + public function testMultiple($content, $expected) + { + // Instanciate the template + $etemplate = new Etemplate(); + $etemplate->read(static::TEST_TEMPLATE, 'test'); + + // Send it around + $result = $this->mockedRoundTrip($etemplate, $content, array( + 'widget' => self::VALUES, + 'widget_multiple' => self::VALUES + )); + + // Test it + $this->validateTest($result, $expected); + } + + public function multipleProvider() + { + return array( + // Test + array( + array('widget' => '', 'widget_multiple' => ''), // Content + array('widget' => '', 'widget_multiple' => ''), // Expected + ), + array( + array('widget' => 'Α', 'widget_multiple' => 'Α'), + array('widget' => 'Α', 'widget_multiple' => 'Α'), + ), + // Check for CSV - should fail + array( + array('widget' => 'Α,Β', 'widget_multiple' => 'Α,Β'), + array('widget' => '', 'widget_multiple' => ''), + ), + // Check for array - should work + array( + array('widget' => array('Α','Β'), 'widget_multiple' => array('Α','Β')), + array('widget' => 'Α', 'widget_multiple' => array('Α','Β')), + ), + ); + } + + /** + * Test that the widget does not return a value if readonly + */ + public function testReadonly() + { + // Instanciate the template + $etemplate = new Etemplate(); + $etemplate->read(static::TEST_TEMPLATE, 'test'); + + // Exec + $content = array( + 'widget' => 'Α', + 'widget_readonly' => 'Α', + 'widget_multiple' => 'Α' + ); + + // Set non-readonly widgets to read-only via parameter to Etemplate::exec() + $result = $this->mockedRoundTrip($etemplate, $content, + array('widget' => self::VALUES, 'widget_readonly' => self::VALUES, 'widget_multiple' => self::VALUES), + array('widget' => true, 'widget_multiple' => true) + ); + + // Check - nothing comes back + $this->assertEquals(array(), $result); + } + + /** + * Test that an edited read-only widget does not return a value, even if the + * client side gives one, which should be an unusual occurrence. + */ + public function testEditedReadonly() + { + // Instanciate the template + $etemplate = new Etemplate(); + $etemplate->read(static::TEST_TEMPLATE, 'test'); + + // Exec + $content = array( + 'widget' => 'Α', + 'widget_readonly' => 'Α', + 'widget_multiple' => 'Α' + ); + $result = $this->mockedExec($etemplate, $content, + array('widget' => self::VALUES, 'widget_readonly' => self::VALUES, 'widget_multiple' => self::VALUES), + array('widget' => true, 'widget_multiple' => true) + ); + + // Check for the load + $data = array(); + foreach($result as $command) + { + if($command['type'] == 'et2_load') + { + $data = $command['data']; + break; + } + } + + // 'Edit' the data client side + $data['data']['content'] = array( + 'widget' => 'Ω', + 'widget_readonly' => 'Ω', + 'widget_multiple' => 'Ω' + ); + + Etemplate::ajax_process_content($data['data']['etemplate_exec_id'], $data['data']['content'], false); + + $content = static::$mocked_exec_result; + static::$mocked_exec_result = array(); + + // Nothing comes back, even though edited since it's readonly + $this->assertEquals(array(), $content); + } +} \ No newline at end of file diff --git a/api/templates/test/select_test.xet b/api/templates/test/select_test.xet new file mode 100644 index 0000000000..ced5fe3113 --- /dev/null +++ b/api/templates/test/select_test.xet @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file