|
|
@@ -8082,4 +8082,264 @@ class FormHelperTest extends TestCase
|
|
|
];
|
|
|
$this->assertHtml($expected, $result);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test the basic setters and getters for value sources
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFormValuesSourcesSettersGetters()
|
|
|
+ {
|
|
|
+ $expected = ['context'];
|
|
|
+ $result = $this->Form->getValuesSources();
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $expected = null;
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '1';
|
|
|
+ $this->Form->request->query['id'] = '2';
|
|
|
+
|
|
|
+ $expected = '1';
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources('query');
|
|
|
+ $expected = ['query'];
|
|
|
+ $result = $this->Form->getValuesSources();
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $expected = '2';
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['data']);
|
|
|
+ $expected = '1';
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['query', 'data']);
|
|
|
+ $expected = '2';
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests the different input rendering values based on sources values switching
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFormValuesSourcesSwitchInputs()
|
|
|
+ {
|
|
|
+ $this->loadFixtures('Articles');
|
|
|
+ $articles = TableRegistry::get('Articles');
|
|
|
+ $article = new Article();
|
|
|
+ $articles->patchEntity($article, ['id' => '3']);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->create($article);
|
|
|
+ $this->Form->setValuesSources(['context']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->query['id'] = '5';
|
|
|
+ $this->Form->setValuesSources(['query']);
|
|
|
+ $this->Form->create($article);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->create($article);
|
|
|
+ $this->Form->setValuesSources(['query']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->query['id'] = '5a';
|
|
|
+ $this->Form->request->data['id'] = '5b';
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['context']);
|
|
|
+ $this->Form->create($article);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['data']);
|
|
|
+ $this->Form->create($article);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '6';
|
|
|
+ $this->Form->request->query['id'] = '7';
|
|
|
+ $this->Form->create($article);
|
|
|
+ $this->Form->setValuesSources(['data']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '6']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '8';
|
|
|
+ $this->Form->request->query['id'] = '9';
|
|
|
+ $this->Form->create($article);
|
|
|
+ $this->Form->setValuesSources(['query']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+
|
|
|
+ unset($this->Form->request->data['id']);
|
|
|
+ $this->Form->request->query['id'] = '9';
|
|
|
+
|
|
|
+ $this->Form->create($article);
|
|
|
+ $this->Form->setValuesSources(['context']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['query']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['context', 'query']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['query', 'context']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['data', 'query', 'context']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '8';
|
|
|
+ $this->Form->request->query['id'] = '9';
|
|
|
+ $this->Form->setValuesSources(['data', 'query', 'context']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test the different form input renderings based on values sources switchings through form options
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFormValuesSourcesSwitchViaOptions()
|
|
|
+ {
|
|
|
+ $this->loadFixtures('Articles');
|
|
|
+ $articles = TableRegistry::get('Articles');
|
|
|
+ $article = new Article();
|
|
|
+ $articles->patchEntity($article, ['id' => '3']);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '4';
|
|
|
+ $this->Form->request->query['id'] = '5';
|
|
|
+
|
|
|
+ $this->Form->create($article, ['valuesSources' => 'query']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals('5', $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '6';
|
|
|
+ $this->Form->request->query['id'] = '7';
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['context']);
|
|
|
+ $this->Form->create($article, ['valuesSources' => 'query']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '7']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals('7', $result);
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['query']);
|
|
|
+ $this->Form->create($article, ['valuesSources' => 'data']);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '6']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals('6', $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '8';
|
|
|
+ $this->Form->request->query['id'] = '9';
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['query']);
|
|
|
+ $this->Form->create($article, ['valuesSources' => ['context', 'data']]);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals('8', $result);
|
|
|
+
|
|
|
+
|
|
|
+ $this->Form->request->data['id'] = '10';
|
|
|
+ $this->Form->request->query['id'] = '11';
|
|
|
+
|
|
|
+ $this->Form->setValuesSources(['context'])->create($article, ['valuesSources' => ['query', 'data']]);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals('11', $result);
|
|
|
+
|
|
|
+ unset($this->Form->request->query['id']);
|
|
|
+ $this->Form->setValuesSources(['context'])->create($article, ['valuesSources' => ['query', 'data']]);
|
|
|
+ $result = $this->Form->input('id');
|
|
|
+ $expected = [
|
|
|
+ ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ $result = $this->Form->getSourceValue('id');
|
|
|
+ $this->assertEquals('10', $result);
|
|
|
+ }
|
|
|
}
|