EGroupware /
egroupware
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Test Etemplate main file |
||
| 5 | * |
||
| 6 | * @link http://www.egroupware.org |
||
| 7 | * @author Nathan Gray |
||
| 8 | * @package api |
||
| 9 | * @copyright (c) 2017 Nathan Gray |
||
| 10 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace EGroupware\Api; |
||
| 14 | |||
| 15 | require_once realpath(__DIR__.'/Etemplate/WidgetBaseTest.php'); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Test the main class of Etemplate |
||
| 19 | * |
||
| 20 | * Etemplate Widget base class scans the apps for widgets, which needs the app |
||
| 21 | * list, pulled from the database, so we need to log in. |
||
| 22 | */ |
||
| 23 | class EtemplateTest extends Etemplate\WidgetBaseTest { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Etemplate checks in the app/template/ directory, so we can't easily |
||
| 27 | * use a specific test template. Using this real template for testing. |
||
| 28 | */ |
||
| 29 | const TEST_TEMPLATE = 'api.prompt'; |
||
| 30 | |||
| 31 | protected $content = array('value' => 'test content'); |
||
| 32 | protected $sel_options = array(array('value' => 0, 'label' => 'label')); |
||
| 33 | protected $readonlys = array('value' => true); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Test reading xml files |
||
| 37 | * |
||
| 38 | * This really just tests that the files can be found and executed. |
||
| 39 | */ |
||
| 40 | public function testRead() |
||
| 41 | { |
||
| 42 | $etemplate = new Etemplate(); |
||
| 43 | |||
| 44 | // Test missing template fails |
||
| 45 | $this->assertEquals(false, $etemplate->read('totally invalid'), 'Reading invalid template'); |
||
| 46 | |||
| 47 | // Templates must be in the correct templates directory - use one from API |
||
| 48 | // This does not actually do anything with the template file |
||
| 49 | $this->assertEquals(true, $etemplate->read(static::TEST_TEMPLATE)); |
||
| 50 | |||
| 51 | // This loads and parses |
||
| 52 | $result = $this->mockedExec($etemplate, array()); |
||
| 53 | |||
| 54 | // Look for the load and match the template name |
||
| 55 | foreach($result as $command) |
||
| 56 | { |
||
| 57 | if($command['type'] == 'et2_load') |
||
| 58 | { |
||
| 59 | $this->assertEquals(static::TEST_TEMPLATE, $command['data']['name']); |
||
| 60 | break; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Test that we can load the etemplate into a different DOM ID than the |
||
| 67 | * default, which is based on the template name. |
||
| 68 | */ |
||
| 69 | public function testSetDOMId() |
||
| 70 | { |
||
| 71 | // Templates must be in the correct templates directory - use one from API |
||
| 72 | $etemplate = new Etemplate(); |
||
| 73 | $etemplate->read(static::TEST_TEMPLATE); |
||
| 74 | |||
| 75 | // Change the target DOM ID |
||
| 76 | $etemplate->set_dom_id('test_id'); |
||
| 77 | |||
| 78 | $result = $this->mockedExec($etemplate, array()); |
||
| 79 | |||
| 80 | // Check for the load |
||
| 81 | foreach($result as $command) |
||
| 82 | { |
||
| 83 | if($command['type'] == 'et2_load') |
||
| 84 | { |
||
| 85 | $this->assertEquals('test_id', $command['data']['DOMNodeID']); |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Test that data that is passed in is passed on |
||
| 93 | */ |
||
| 94 | public function testExec() |
||
| 95 | { |
||
| 96 | // Templates must be in the correct templates directory - use one from API |
||
| 97 | $etemplate = new Etemplate(); |
||
| 98 | $etemplate->read(static::TEST_TEMPLATE); |
||
| 99 | |||
| 100 | // Change the target DOM ID |
||
| 101 | $etemplate->set_dom_id('test_id'); |
||
| 102 | |||
| 103 | $result = $this->mockedExec($etemplate, $this->content, $this->sel_options, $this->readonlys); |
||
| 104 | |||
| 105 | // Check for the load |
||
| 106 | $data = array(); |
||
| 107 | foreach($result as $command) |
||
| 108 | { |
||
| 109 | if($command['type'] == 'et2_load') |
||
| 110 | { |
||
| 111 | $data = $command['data']; |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->assertArraySubset($this->content, $data['data']['content'], false, 'Content does not match'); |
||
| 117 | $this->assertArraySubset($this->sel_options, $data['data']['sel_options'], false, 'Select options do not match'); |
||
| 118 | $this->assertArraySubset($this->readonlys, $data['data']['readonlys'], false, 'Readonlys does not match'); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Test that data passed in is passed back |
||
| 123 | * |
||
| 124 | * In this case, since there's one input widget and we're passing it's value, and |
||
| 125 | * we're not passing anything extra and no preserve, it should be the same. |
||
| 126 | * |
||
| 127 | * @depends testExec |
||
| 128 | */ |
||
| 129 | public function testRoundTrip() |
||
| 130 | { |
||
| 131 | // Templates must be in the correct templates directory - use one from API |
||
| 132 | $etemplate = new Etemplate(); |
||
| 133 | $etemplate->read(static::TEST_TEMPLATE); |
||
| 134 | |||
| 135 | $this->readonlys['value'] = false; |
||
| 136 | |||
| 137 | $result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys); |
||
| 138 | |||
| 139 | $this->assertEquals($this->content, $result); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Simple test of a read-only widget |
||
| 144 | * |
||
| 145 | * The value is passed in, but does not come back |
||
| 146 | * |
||
| 147 | * @depends testExec |
||
| 148 | */ |
||
| 149 | public function testSimpleReadonly() |
||
| 150 | { |
||
| 151 | // Templates must be in the correct templates directory - use one from API |
||
| 152 | $etemplate = new Etemplate(); |
||
| 153 | $etemplate->read(static::TEST_TEMPLATE); |
||
| 154 | |||
| 155 | $this->readonlys['value'] = true; |
||
| 156 | |||
| 157 | $result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys); |
||
| 158 | |||
| 159 | // The only input widget is readonly, expect an empty array |
||
| 160 | $this->assertEquals(array(), $result); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Simple test of preserve |
||
| 165 | * |
||
| 166 | * The value is passed in, and comes back, even if the widget is readonly, |
||
| 167 | * or if there is no matching widget. |
||
| 168 | * |
||
| 169 | * @depends testExec |
||
| 170 | */ |
||
| 171 | public function testArbitraryPreserve() |
||
| 172 | { |
||
| 173 | // Templates must be in the correct templates directory - use one from API |
||
| 174 | $etemplate = new Etemplate(); |
||
| 175 | $etemplate->read(static::TEST_TEMPLATE); |
||
| 176 | |||
| 177 | $this->readonlys['value'] = true; |
||
| 178 | |||
| 179 | $preserve = array('arbitrary' => 'value'); |
||
| 180 | $result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve); |
||
| 181 | |||
| 182 | // The only input widget is readonly, expect preserve back |
||
| 183 | $this->assertEquals($preserve, $result); |
||
| 184 | |||
| 185 | // Now try with widget |
||
| 186 | $this->readonlys['value'] = false; |
||
| 187 | |||
| 188 | $result2 = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve); |
||
| 189 | |||
| 190 | // The only input widget is readonly, expect preserve + content back |
||
| 191 | $this->assertArraySubset($this->content, $result2); |
||
| 192 | $this->assertArraySubset($preserve, $result2); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Test of editable widget value overriding preserved value but a readonly |
||
| 197 | * widget does not override preserved value. |
||
| 198 | */ |
||
| 199 | public function testReadonlyPreserve() |
||
| 200 | { |
||
| 201 | $etemplate = new Etemplate(); |
||
| 202 | $etemplate->read(static::TEST_TEMPLATE); |
||
| 203 | |||
| 204 | $this->readonlys['value'] = true; |
||
| 205 | $preserve['value'] = 'preserved_value'; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 206 | |||
| 207 | $result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve); |
||
| 208 | |||
| 209 | // The only input widget is readonly, expect preserve back, not content |
||
| 210 | $this->assertEquals($preserve['value'], $result['value']); |
||
| 211 | |||
| 212 | $this->readonlys['value'] = false; |
||
| 213 | $result2 = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve); |
||
| 214 | |||
| 215 | // The only input widget is editable, expect content back, not preserve |
||
| 216 | $this->assertEquals($this->content['value'], $result2['value']); |
||
| 217 | } |
||
| 218 | } |
||
| 219 |