From dd8551e3c4b415a3ac4031f4d2aa0e57b8721028 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Sun, 24 Dec 2017 05:32:01 -0200 Subject: [PATCH 01/69] Escape Forge attributes --- system/Database/Forge.php | 2 +- system/Database/MySQLi/Forge.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/system/Database/Forge.php b/system/Database/Forge.php index d7b7eb5a87ee..f3cdc1cbacc0 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -528,7 +528,7 @@ protected function _createTableAttributes($attributes) { if (is_string($key)) { - $sql .= ' ' . strtoupper($key) . ' ' . $attributes[$key]; + $sql .= ' ' . strtoupper($key) . ' ' . $this->db->escape($attributes[$key]); } } diff --git a/system/Database/MySQLi/Forge.php b/system/Database/MySQLi/Forge.php index 79093694aefa..562ae19b24be 100644 --- a/system/Database/MySQLi/Forge.php +++ b/system/Database/MySQLi/Forge.php @@ -55,7 +55,7 @@ class Forge extends \CodeIgniter\Database\Forge * @var string */ protected $dropConstraintStr = 'ALTER TABLE %s DROP FOREIGN KEY %s'; - + /** * CREATE TABLE keys flag * @@ -109,18 +109,18 @@ protected function _createTableAttributes($attributes) { if (is_string($key)) { - $sql .= ' ' . strtoupper($key) . ' = ' . $attributes[$key]; + $sql .= ' ' . strtoupper($key) . ' = ' . $this->db->escape($attributes[$key]); } } if ( ! empty($this->db->charset) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET')) { - $sql .= ' DEFAULT CHARACTER SET = ' . $this->db->charset; + $sql .= ' DEFAULT CHARACTER SET = ' . $this->db->escape($this->db->charset); } if ( ! empty($this->db->DBCollat) && ! strpos($sql, 'COLLATE')) { - $sql .= ' COLLATE = ' . $this->db->DBCollat; + $sql .= ' COLLATE = ' . $this->db->escape($this->db->DBCollat); } return $sql; From 2a181d1e66e9c596f0dac9a9e7ff43d4a5b62c52 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Sun, 24 Dec 2017 07:48:23 -0200 Subject: [PATCH 02/69] Test Forge createTable with attributes --- system/Database/Postgre/Forge.php | 13 +++++++++++++ tests/system/Database/Live/ForgeTest.php | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/system/Database/Postgre/Forge.php b/system/Database/Postgre/Forge.php index ebfbe14a31d6..e5a0ac4fa0f0 100644 --- a/system/Database/Postgre/Forge.php +++ b/system/Database/Postgre/Forge.php @@ -76,6 +76,19 @@ class Forge extends \CodeIgniter\Database\Forge //-------------------------------------------------------------------- + /** + * CREATE TABLE attributes + * + * @param array $attributes Associative array of table attributes + * @return string + */ + protected function _createTableAttributes($attributes) + { + return ''; + } + + //-------------------------------------------------------------------- + /** * ALTER TABLE * diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index 143683ac5664..411e90d0c90e 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -37,6 +37,24 @@ public function testCreateTable() $this->assertTrue($exist); } + public function testCreateTableWithAttributes() + { + $this->forge->dropTable('forge_test_attributes', true); + + $this->forge->addField('id'); + + $attributes = [ + 'comment' => "Forge's Test" + ]; + + $this->forge->createTable('forge_test_attributes', false, $attributes); + + $exist = $this->db->tableExists('forge_test_attributes'); + $this->forge->dropTable('forge_test_attributes', true, true); + + $this->assertTrue($exist); + } + public function testAddFields() { From 684cfbb028806fdfb1ccb3f9b65ab447fcb6f635 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Wed, 25 Apr 2018 22:09:34 -0300 Subject: [PATCH 03/69] Add default namespace in the migration example doc --- user_guide_src/source/database/migration.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/database/migration.rst b/user_guide_src/source/database/migration.rst index 0557ea6aa54f..3a4626fe3c3e 100644 --- a/user_guide_src/source/database/migration.rst +++ b/user_guide_src/source/database/migration.rst @@ -54,7 +54,7 @@ migrations go in the **application/Database/Migrations/** directory and have nam as *20121031100537_Add_blog.php*. :: - Date: Mon, 25 Jun 2018 19:45:03 +0800 Subject: [PATCH 04/69] Update Logger to cache log handler. --- system/Log/Logger.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/system/Log/Logger.php b/system/Log/Logger.php index 4de39d1bc869..36cfec803399 100644 --- a/system/Log/Logger.php +++ b/system/Log/Logger.php @@ -364,10 +364,14 @@ public function log($level, $message, array $context = []): bool foreach ($this->handlerConfig as $className => $config) { + if ( ! array_key_exists($className, $this->handlers)) { + $this->handlers[$className] = new $className($config); + } + /** * @var \CodeIgniter\Log\Handlers\HandlerInterface */ - $handler = new $className($config); + $handler = $this->handlers[$className]; if ( ! $handler->canHandle($level)) { From e55cae1e145bcd8853742478eaafe336ea9f9153 Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Wed, 27 Jun 2018 08:28:09 -0700 Subject: [PATCH 05/69] Form helper testing --- system/Helpers/form_helper.php | 2 +- tests/system/Helpers/FormHelperTest.php | 913 +++++++++++++----------- 2 files changed, 496 insertions(+), 419 deletions(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index dcb7ab750d7e..8705082f2452 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -82,7 +82,7 @@ function form_open(string $action = '', array $attributes = [], array $hidden = $form = '
\n"; // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites - $before = (new \Config\Filters())->globals['before']; + $before = Services::filters()->getFilters()['before']; if ((in_array('csrf', $before) || array_key_exists('csrf', $before)) && strpos($action, base_url()) !== false && ! stripos($form, 'method="get"') ) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 955ee82850db..560b1d8e3b87 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -1,4 +1,6 @@ -baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); + + public function setUp() + { + parent::setUp(); + + helper('form'); + } + + // ------------------------------------------------------------------------ + public function testFormOpenBasic() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); $before = (new Filters())->globals['before']; - if (in_array('csrf', $before) || array_key_exists('csrf', $before )) + if (in_array('csrf', $before) || array_key_exists('csrf', $before)) { $Value = csrf_hash(); $Name = csrf_token(); @@ -43,26 +47,27 @@ public function testFormOpenBasic() EOH; } - $attributes = [ - 'name' => 'form', - 'id' => 'form', - 'method' => 'POST' - ]; - $this->assertEquals($expected, form_open('foo/bar', $attributes)); - } - // ------------------------------------------------------------------------ - public function testFormOpenWithoutAction() - { - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); + $attributes = [ + 'name' => 'form', + 'id' => 'form', + 'method' => 'POST' + ]; + $this->assertEquals($expected, form_open('foo/bar', $attributes)); + } + + // ------------------------------------------------------------------------ + public function testFormOpenWithoutAction() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); $before = (new Filters())->globals['before']; - if (in_array('csrf', $before) || array_key_exists('csrf', $before )) + if (in_array('csrf', $before) || array_key_exists('csrf', $before)) { $Value = csrf_hash(); $Name = csrf_token(); @@ -79,26 +84,27 @@ public function testFormOpenWithoutAction() EOH; } - $attributes = [ - 'name' => 'form', - 'id' => 'form', - 'method' => 'POST' - ]; - $this->assertEquals($expected, form_open('', $attributes)); - } - // ------------------------------------------------------------------------ - public function testFormOpenWithoutMethod() - { - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); + $attributes = [ + 'name' => 'form', + 'id' => 'form', + 'method' => 'POST' + ]; + $this->assertEquals($expected, form_open('', $attributes)); + } + + // ------------------------------------------------------------------------ + public function testFormOpenWithoutMethod() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); $before = (new Filters())->globals['before']; - if (in_array('csrf', $before) || array_key_exists('csrf', $before )) + if (in_array('csrf', $before) || array_key_exists('csrf', $before)) { $Value = csrf_hash(); $Name = csrf_token(); @@ -116,25 +122,26 @@ public function testFormOpenWithoutMethod() EOH; } - $attributes = [ - 'name' => 'form', - 'id' => 'form' - ]; - $this->assertEquals($expected, form_open('foo/bar', $attributes)); - } - // ------------------------------------------------------------------------ - public function testFormOpenWithHidden() - { - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); + $attributes = [ + 'name' => 'form', + 'id' => 'form' + ]; + $this->assertEquals($expected, form_open('foo/bar', $attributes)); + } + + // ------------------------------------------------------------------------ + public function testFormOpenWithHidden() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); $before = (new Filters())->globals['before']; - if (in_array('csrf', $before) || array_key_exists('csrf', $before )) + if (in_array('csrf', $before) || array_key_exists('csrf', $before)) { $Value = csrf_hash(); $Name = csrf_token(); @@ -154,29 +161,65 @@ public function testFormOpenWithHidden() EOH; } - $attributes = [ - 'name' => 'form', - 'id' => 'form', - 'method' => 'POST' - ]; - $hidden = [ - 'foo' => 'bar' - ]; - $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); - } - // ------------------------------------------------------------------------ - public function testFormOpenMultipart() - { - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); + $attributes = [ + 'name' => 'form', + 'id' => 'form', + 'method' => 'POST' + ]; + $hidden = [ + 'foo' => 'bar' + ]; + $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); + } + +// ------------------------------------------------------------------------ + public function testFormOpenWithCSRF() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); + + $filters = Services::filters(); + $filters->globals['before'][] = 'csrf'; // force CSRF + $before = $filters->globals['before']; + + $Value = csrf_hash(); + $Name = csrf_token(); + $expected = << + + + +EOH; + + $attributes = [ + 'name' => 'form', + 'id' => 'form', + 'method' => 'POST' + ]; + $hidden = [ + 'foo' => 'bar' + ]; + $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); + } + + // ------------------------------------------------------------------------ + public function testFormOpenMultipart() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); $before = (new Filters())->globals['before']; - if (in_array('csrf', $before) || array_key_exists('csrf', $before )) + if (in_array('csrf', $before) || array_key_exists('csrf', $before)) { $Value = csrf_hash(); $Name = csrf_token(); @@ -193,105 +236,114 @@ public function testFormOpenMultipart() EOH; } - $attributes = [ - 'name' => 'form', - 'id' => 'form', - 'method' => 'POST' - ]; - $this->assertEquals($expected, form_open_multipart('foo/bar', $attributes)); - } - // ------------------------------------------------------------------------ - public function testFormHidden() - { - $expected = << 'form', + 'id' => 'form', + 'method' => 'POST' + ]; + $this->assertEquals($expected, form_open_multipart('foo/bar', $attributes)); + } + + // ------------------------------------------------------------------------ + public function testFormHidden() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_hidden('username', 'johndoe')); - } - // ------------------------------------------------------------------------ - public function testFormHiddenArrayInput() - { - $data = [ - 'foo' => 'bar' - ]; - $expected = <<assertEquals($expected, form_hidden('username', 'johndoe')); + } + + // ------------------------------------------------------------------------ + public function testFormHiddenArrayInput() + { + $data = [ + 'foo' => 'bar' + ]; + $expected = << EOH; - $this->assertEquals($expected, form_hidden($data, null)); - } - // ------------------------------------------------------------------------ - public function testFormHiddenArrayValues() - { - $data = [ - 'foo' => 'bar' - ]; - $expected = <<assertEquals($expected, form_hidden($data, null)); + } + + // ------------------------------------------------------------------------ + public function testFormHiddenArrayValues() + { + $data = [ + 'foo' => 'bar' + ]; + $expected = << EOH; - $this->assertEquals($expected, form_hidden('name', $data)); - } - // ------------------------------------------------------------------------ - public function testFormInput() - { - $expected = <<assertEquals($expected, form_hidden('name', $data)); + } + + // ------------------------------------------------------------------------ + public function testFormInput() + { + $expected = <<\n EOH; - $data = [ - 'name' => 'username', - 'id' => 'username', - 'value' => 'johndoe', - 'maxlength' => '100', - 'size' => '50', - 'style' => 'width:50%', - ]; - $this->assertEquals($expected, form_input($data)); - } - // ------------------------------------------------------------------------ - public function test_form_password() - { - $expected = << 'username', + 'id' => 'username', + 'value' => 'johndoe', + 'maxlength' => '100', + 'size' => '50', + 'style' => 'width:50%', + ]; + $this->assertEquals($expected, form_input($data)); + } + + // ------------------------------------------------------------------------ + public function test_form_password() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_password('password')); - } - // ------------------------------------------------------------------------ - public function test_form_upload() - { - $expected = <<assertEquals($expected, form_password('password')); + } + + // ------------------------------------------------------------------------ + public function test_form_upload() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_upload('attachment')); - } - // ------------------------------------------------------------------------ - public function test_form_textarea() - { - $expected = <<assertEquals($expected, form_upload('attachment')); + } + + // ------------------------------------------------------------------------ + public function test_form_textarea() + { + $expected = <<Notes\n EOH; - $this->assertEquals($expected, form_textarea('notes', 'Notes')); - } - // ------------------------------------------------------------------------ - public function testFormTextareaWithValueAttribute() - { - $data = [ - 'name' => 'foo', - 'value' => 'bar' - ]; - $expected = <<assertEquals($expected, form_textarea('notes', 'Notes')); + } + + // ------------------------------------------------------------------------ + public function testFormTextareaWithValueAttribute() + { + $data = [ + 'name' => 'foo', + 'value' => 'bar' + ]; + $expected = <<bar EOH; - $this->assertEquals($expected, form_textarea($data)); - } - // ------------------------------------------------------------------------ - public function test_form_dropdown() - { - $expected = <<assertEquals($expected, form_textarea($data)); + } + + // ------------------------------------------------------------------------ + public function test_form_dropdown() + { + $expected = << @@ -299,14 +351,14 @@ public function test_form_dropdown() \n EOH; - $options = [ - 'small' => 'Small Shirt', - 'med' => 'Medium Shirt', - 'large' => 'Large Shirt', - 'xlarge' => 'Extra Large Shirt', - ]; - $this->assertEquals($expected, form_dropdown('shirts', $options, 'large')); - $expected = << 'Small Shirt', + 'med' => 'Medium Shirt', + 'large' => 'Large Shirt', + 'xlarge' => 'Extra Large Shirt', + ]; + $this->assertEquals($expected, form_dropdown('shirts', $options, 'large')); + $expected = << @@ -314,19 +366,19 @@ public function test_form_dropdown() \n EOH; - $shirts_on_sale = ['small', 'large']; - $this->assertEquals($expected, form_dropdown('shirts', $options, $shirts_on_sale)); - $options = [ - 'Swedish Cars' => [ - 'volvo' => 'Volvo', - 'saab' => 'Saab' - ], - 'German Cars' => [ - 'mercedes' => 'Mercedes', - 'audi' => 'Audi' - ] - ]; - $expected = <<assertEquals($expected, form_dropdown('shirts', $options, $shirts_on_sale)); + $options = [ + 'Swedish Cars' => [ + 'volvo' => 'Volvo', + 'saab' => 'Saab' + ], + 'German Cars' => [ + 'mercedes' => 'Mercedes', + 'audi' => 'Audi' + ] + ]; + $expected = << @@ -338,60 +390,64 @@ public function test_form_dropdown() \n EOH; - $this->assertEquals($expected, form_dropdown('cars', $options, ['volvo', 'audi'])); - } - // ------------------------------------------------------------------------ - public function testFormDropdownWithSelectedAttribute() - { - $expected = <<assertEquals($expected, form_dropdown('cars', $options, ['volvo', 'audi'])); + } + + // ------------------------------------------------------------------------ + public function testFormDropdownWithSelectedAttribute() + { + $expected = << EOH; - $data = [ - 'name' => 'foo', - 'selected' => 'bar' - ]; - $options = [ - 'bar' => 'Bar' - ]; - $this->assertEquals($expected, form_dropdown($data, $options)); - } - // ------------------------------------------------------------------------ - public function testFormDropdownWithOptionsAttribute() - { - $expected = << 'foo', + 'selected' => 'bar' + ]; + $options = [ + 'bar' => 'Bar' + ]; + $this->assertEquals($expected, form_dropdown($data, $options)); + } + + // ------------------------------------------------------------------------ + public function testFormDropdownWithOptionsAttribute() + { + $expected = << EOH; - $data = [ - 'name' => 'foo', - 'options' => [ - 'bar' => 'Bar' - ] - ]; - $this->assertEquals($expected, form_dropdown($data)); - } - // ------------------------------------------------------------------------ - public function testFormDropdownWithEmptyArrayOptionValue() - { - $expected = << 'foo', + 'options' => [ + 'bar' => 'Bar' + ] + ]; + $this->assertEquals($expected, form_dropdown($data)); + } + + // ------------------------------------------------------------------------ + public function testFormDropdownWithEmptyArrayOptionValue() + { + $expected = << EOH; - $options = [ - 'bar' => [] - ]; - $this->assertEquals($expected, form_dropdown('foo', $options)); - } - // ------------------------------------------------------------------------ - public function test_form_multiselect() - { - $expected = << [] + ]; + $this->assertEquals($expected, form_dropdown('foo', $options)); + } + + // ------------------------------------------------------------------------ + public function test_form_multiselect() + { + $expected = << @@ -399,169 +455,185 @@ public function test_form_multiselect() \n EOH; - $options = [ - 'small' => 'Small Shirt', - 'med' => 'Medium Shirt', - 'large' => 'Large Shirt', - 'xlarge' => 'Extra Large Shirt', - ]; - $this->assertEquals($expected, form_multiselect('shirts[]', $options, ['med', 'large'])); - } - // ------------------------------------------------------------------------ - public function test_form_fieldset() - { - $expected = << 'Small Shirt', + 'med' => 'Medium Shirt', + 'large' => 'Large Shirt', + 'xlarge' => 'Extra Large Shirt', + ]; + $this->assertEquals($expected, form_multiselect('shirts[]', $options, ['med', 'large'])); + } + + // ------------------------------------------------------------------------ + public function test_form_fieldset() + { + $expected = << Address Information\n EOH; - $this->assertEquals($expected, form_fieldset('Address Information')); - } - // ------------------------------------------------------------------------ - public function testFormFieldsetWithNoLegent() - { - $expected = <<assertEquals($expected, form_fieldset('Address Information')); + } + + // ------------------------------------------------------------------------ + public function testFormFieldsetWithNoLegent() + { + $expected = << EOH; - $this->assertEquals($expected, form_fieldset()); - } - // ------------------------------------------------------------------------ - public function testFormFieldsetWithAttributes() - { - $attributes = [ - 'name' => 'bar' - ]; - $expected = <<assertEquals($expected, form_fieldset()); + } + + // ------------------------------------------------------------------------ + public function testFormFieldsetWithAttributes() + { + $attributes = [ + 'name' => 'bar' + ]; + $expected = << Foo EOH; - $this->assertEquals($expected, form_fieldset('Foo', $attributes)); - } - // ------------------------------------------------------------------------ - public function test_form_fieldset_close() - { - $expected = <<assertEquals($expected, form_fieldset('Foo', $attributes)); + } + + // ------------------------------------------------------------------------ + public function test_form_fieldset_close() + { + $expected = << EOH; - $this->assertEquals($expected, form_fieldset_close('')); - } - // ------------------------------------------------------------------------ - public function test_form_checkbox() - { - $expected = <<assertEquals($expected, form_fieldset_close('')); + } + + // ------------------------------------------------------------------------ + public function test_form_checkbox() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_checkbox('newsletter', 'accept', TRUE)); - } - // ------------------------------------------------------------------------ - public function testFormCheckboxArrayData() - { - $data = [ - 'name' => 'foo', - 'value' => 'bar', - 'checked' => true - ]; - $expected = <<assertEquals($expected, form_checkbox('newsletter', 'accept', TRUE)); + } + + // ------------------------------------------------------------------------ + public function testFormCheckboxArrayData() + { + $data = [ + 'name' => 'foo', + 'value' => 'bar', + 'checked' => true + ]; + $expected = << EOH; - $this->assertEquals($expected, form_checkbox($data)); - } - // ------------------------------------------------------------------------ - public function testFormCheckboxArrayDataWithCheckedFalse() - { - $data = [ - 'name' => 'foo', - 'value' => 'bar', - 'checked' => false - ]; - $expected = <<assertEquals($expected, form_checkbox($data)); + } + + // ------------------------------------------------------------------------ + public function testFormCheckboxArrayDataWithCheckedFalse() + { + $data = [ + 'name' => 'foo', + 'value' => 'bar', + 'checked' => false + ]; + $expected = << EOH; - $this->assertEquals($expected, form_checkbox($data)); - } - // ------------------------------------------------------------------------ - public function test_form_radio() - { - $expected = <<assertEquals($expected, form_checkbox($data)); + } + + // ------------------------------------------------------------------------ + public function test_form_radio() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_radio('newsletter', 'accept', TRUE)); - } - // ------------------------------------------------------------------------ - public function test_form_submit() - { - $expected = <<assertEquals($expected, form_radio('newsletter', 'accept', TRUE)); + } + + // ------------------------------------------------------------------------ + public function test_form_submit() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_submit('mysubmit', 'Submit Post!')); - } - // ------------------------------------------------------------------------ - public function test_form_label() - { - $expected = <<assertEquals($expected, form_submit('mysubmit', 'Submit Post!')); + } + + // ------------------------------------------------------------------------ + public function test_form_label() + { + $expected = <<What is your Name EOH; - $this->assertEquals($expected, form_label('What is your Name', 'username')); - } - // ------------------------------------------------------------------------ - public function testFormLabelWithAttributes() - { - $attributes = [ - 'id' => 'label1' - ]; - $expected = <<assertEquals($expected, form_label('What is your Name', 'username')); + } + + // ------------------------------------------------------------------------ + public function testFormLabelWithAttributes() + { + $attributes = [ + 'id' => 'label1' + ]; + $expected = <<bar EOH; - $this->assertEquals($expected, form_label('bar', 'foo', $attributes)); - } - // ------------------------------------------------------------------------ - public function test_form_reset() - { - $expected = <<assertEquals($expected, form_label('bar', 'foo', $attributes)); + } + + // ------------------------------------------------------------------------ + public function test_form_reset() + { + $expected = <<\n EOH; - $this->assertEquals($expected, form_reset('myreset', 'Reset')); - } - // ------------------------------------------------------------------------ - public function test_form_button() - { - $expected = <<assertEquals($expected, form_reset('myreset', 'Reset')); + } + + // ------------------------------------------------------------------------ + public function test_form_button() + { + $expected = <<content\n EOH; - $this->assertEquals($expected, form_button('name', 'content')); - } - // ------------------------------------------------------------------------ - public function testFormButtonWithDataArray() - { - $data = [ - 'name' => 'foo', - 'content' => 'bar' - ]; - $expected = <<assertEquals($expected, form_button('name', 'content')); + } + + // ------------------------------------------------------------------------ + public function testFormButtonWithDataArray() + { + $data = [ + 'name' => 'foo', + 'content' => 'bar' + ]; + $expected = <<bar EOH; - $this->assertEquals($expected, form_button($data)); - } - // ------------------------------------------------------------------------ - public function test_form_close() - { - $expected = <<assertEquals($expected, form_button($data)); + } + + // ------------------------------------------------------------------------ + public function test_form_close() + { + $expected = << EOH; - $this->assertEquals($expected, form_close('')); - } - // ------------------------------------------------------------------------ - public function testFormDatalist() - { - $options = [ - 'foo1', - 'bar1' - ]; - $expected = <<assertEquals($expected, form_close('')); + } + + // ------------------------------------------------------------------------ + public function testFormDatalist() + { + $options = [ + 'foo1', + 'bar1' + ]; + $expected = << EOH; - $this->assertEquals($expected, form_datalist('foo', 'bar', $options)); - } - // ------------------------------------------------------------------------ - public function test_set_value() - { - $_SESSION['_ci_old_input']['post']['foo'] = 'assertEquals('<bar', set_value('foo')); - - unset($_SESSION['_ci_old_input']['post']['foo']); - $this->assertEquals('baz', set_value('foo', 'baz')); - } - // ------------------------------------------------------------------------ - public function test_set_select() - { - $_SESSION['_ci_old_input']['post']['foo'] = 'bar'; - $this->assertEquals(' selected="selected"', set_select('foo', 'bar')); - - $_SESSION['_ci_old_input']['post']['foo'] = ['foo' => 'bar']; - $this->assertEquals(' selected="selected"', set_select('foo', 'bar')); - $this->assertEquals('', set_select('foo', 'baz')); - - unset($_SESSION['_ci_old_input']['post']['foo']); - $this->assertEquals(' selected="selected"', set_select('foo', 'baz', true)); - } - // ------------------------------------------------------------------------ - public function test_set_checkbox() - { - $_SESSION = [ - '_ci_old_input' => [ - 'post' => [ - 'foo' => 'bar' - ] - ] - ]; - - $this->assertEquals(' checked="checked"', set_checkbox('foo', 'bar')); - - $_SESSION = [ - '_ci_old_input' => [ - 'post' => [ - 'foo' => ['foo' => 'bar'] - ] - ] - ]; - $this->assertEquals(' checked="checked"', set_checkbox('foo', 'bar')); - $this->assertEquals('', set_checkbox('foo', 'baz')); + $this->assertEquals($expected, form_datalist('foo', 'bar', $options)); + } + + // ------------------------------------------------------------------------ + public function test_set_value() + { + $_SESSION['_ci_old_input']['post']['foo'] = 'assertEquals('<bar', set_value('foo')); + + unset($_SESSION['_ci_old_input']['post']['foo']); + $this->assertEquals('baz', set_value('foo', 'baz')); + } + + // ------------------------------------------------------------------------ + public function test_set_select() + { + $_SESSION['_ci_old_input']['post']['foo'] = 'bar'; + $this->assertEquals(' selected="selected"', set_select('foo', 'bar')); + + $_SESSION['_ci_old_input']['post']['foo'] = ['foo' => 'bar']; + $this->assertEquals(' selected="selected"', set_select('foo', 'bar')); + $this->assertEquals('', set_select('foo', 'baz')); + + unset($_SESSION['_ci_old_input']['post']['foo']); + $this->assertEquals(' selected="selected"', set_select('foo', 'baz', true)); + } + + // ------------------------------------------------------------------------ + public function test_set_checkbox() + { + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'foo' => 'bar' + ] + ] + ]; + + $this->assertEquals(' checked="checked"', set_checkbox('foo', 'bar')); + + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'foo' => ['foo' => 'bar'] + ] + ] + ]; + $this->assertEquals(' checked="checked"', set_checkbox('foo', 'bar')); + $this->assertEquals('', set_checkbox('foo', 'baz')); $_SESSION = []; - $this->assertEquals('', set_checkbox('foo', 'bar')); - } - // ------------------------------------------------------------------------ - public function test_set_radio() - { - $_SESSION = [ - '_ci_old_input' => [ - 'post' => [ - 'foo' => 'bar' - ] - ] - ]; - - $this->assertEquals(' checked="checked"', set_radio('foo', 'bar')); - $this->assertEquals('', set_radio('foo', 'baz')); - } + $this->assertEquals('', set_checkbox('foo', 'bar')); + } + + // ------------------------------------------------------------------------ + public function test_set_radio() + { + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'foo' => 'bar' + ] + ] + ]; + + $this->assertEquals(' checked="checked"', set_radio('foo', 'bar')); + $this->assertEquals('', set_radio('foo', 'baz')); + } + } From c305411281119ec5c53ed5e7e44cc28041494d39 Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Thu, 28 Jun 2018 09:08:09 -0700 Subject: [PATCH 06/69] FormHelperTestexcept for FormOpenWithCSRF --- system/Helpers/form_helper.php | 58 +++--- tests/system/Helpers/FormHelperTest.php | 191 +++++++++++++----- user_guide_src/source/helpers/form_helper.rst | 13 +- 3 files changed, 176 insertions(+), 86 deletions(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 8705082f2452..2b847d174ca3 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -48,12 +48,12 @@ * Creates the opening portion of the form. * * @param string $action the URI segments of the form destination - * @param array $attributes a key/value pair of attributes + * @param array|string $attributes a key/value pair of attributes, or string representation * @param array $hidden a key/value pair hidden data * * @return string */ - function form_open(string $action = '', array $attributes = [], array $hidden = []): string + function form_open(string $action = '', $attributes = [], array $hidden = []): string { // If no action is provided then set to the current url if ( ! $action) @@ -114,16 +114,16 @@ function form_open(string $action = '', array $attributes = [], array $hidden = * Creates the opening portion of the form, but with "multipart/form-data". * * @param string $action The URI segments of the form destination - * @param array $attributes A key/value pair of attributes + * @param array|string $attributes A key/value pair of attributes, or the same as a string * @param array $hidden A key/value pair hidden data * * @return string */ - function form_open_multipart(string $action = '', array $attributes = [], array $hidden = []): string + function form_open_multipart(string $action = '', $attributes = [], array $hidden = []): string { if (is_string($attributes)) { - $attributes .= ' enctype="multipart/form-data"'; + $attributes .= ' enctype="' . esc('multipart/form-data', 'attr') . '"'; } else { @@ -208,9 +208,9 @@ function form_hidden($name, $value, bool $recursing = false): string function form_input($data = '', string $value = '', $extra = '', string $type = 'text'): string { $defaults = [ - 'type' => $type, - 'name' => is_array($data) ? '' : $data, - 'value' => $value, + 'type' => $type, + 'name' => is_array($data) ? '' : $data, + 'value' => $value, ]; return '\n"; @@ -288,9 +288,9 @@ function form_upload($data = '', string $value = '', $extra = ''): string function form_textarea($data = '', string $value = '', $extra = ''): string { $defaults = [ - 'name' => is_array($data) ? '' : $data, - 'cols' => '40', - 'rows' => '10', + 'name' => is_array($data) ? '' : $data, + 'cols' => '40', + 'rows' => '10', ]; if ( ! is_array($data) || ! isset($data['value'])) { @@ -515,9 +515,9 @@ function form_radio($data = '', string $value = '', bool $checked = false, $extr function form_submit($data = '', string $value = '', $extra = ''): string { $defaults = [ - 'type' => 'submit', - 'name' => is_array($data) ? '' : $data, - 'value' => $value, + 'type' => 'submit', + 'name' => is_array($data) ? '' : $data, + 'value' => $value, ]; return '\n"; @@ -542,9 +542,9 @@ function form_submit($data = '', string $value = '', $extra = ''): string function form_reset($data = '', string $value = '', $extra = ''): string { $defaults = [ - 'type' => 'reset', - 'name' => is_array($data) ? '' : $data, - 'value' => $value, + 'type' => 'reset', + 'name' => is_array($data) ? '' : $data, + 'value' => $value, ]; return '\n"; @@ -569,8 +569,8 @@ function form_reset($data = '', string $value = '', $extra = ''): string function form_button($data = '', string $content = '', $extra = ''): string { $defaults = [ - 'name' => is_array($data) ? '' : $data, - 'type' => 'button', + 'name' => is_array($data) ? '' : $data, + 'type' => 'button', ]; if (is_array($data) && isset($data['content'])) @@ -643,10 +643,10 @@ function form_label(string $label_text = '', string $id = '', array $attributes function form_datalist($name, $value, $options) { $data = [ - 'type' => 'text', - 'name' => $name, - 'list' => $name . '_list', - 'value' => $value, + 'type' => 'text', + 'name' => $name, + 'list' => $name . '_list', + 'value' => $value, ]; $out = form_input($data) . "\n"; @@ -868,7 +868,7 @@ function set_checkbox(string $field, string $value = '', bool $default = false): } // Unchecked checkbox and radio inputs are not even submitted by browsers ... - if (! empty($request->getPost()) || ! empty(old($field))) + if ( ! empty($request->getPost()) || ! empty(old($field))) { return ($input === $value) ? ' checked="checked"' : ''; } @@ -904,7 +904,6 @@ function set_radio(string $field, string $value = '', bool $default = false): st // Try any old input data we may have first $input = $request->getOldInput($field); - if ($input === null) { $input = $request->getPost($field) ?? $default; @@ -925,12 +924,15 @@ function set_radio(string $field, string $value = '', bool $default = false): st } // Unchecked checkbox and radio inputs are not even submitted by browsers ... + $result = ''; if ($request->getPost()) { - return ($input === $value) ? ' checked="checked"' : ''; + $result = ($input === $value) ? ' checked="checked"' : ''; } - return ($default === true) ? ' checked="checked"' : ''; + if (empty($result)) + $result = ($default === true) ? ' checked="checked"' : ''; + return $result; } } @@ -962,7 +964,7 @@ function parse_form_attributes($attributes, $default): string unset($attributes[$key]); } } - if (! empty($attributes)) + if ( ! empty($attributes)) { $default = array_merge($default, $attributes); } diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 560b1d8e3b87..daf865189497 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -173,40 +173,40 @@ public function testFormOpenWithHidden() } // ------------------------------------------------------------------------ - public function testFormOpenWithCSRF() - { - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $filters = Services::filters(); - $filters->globals['before'][] = 'csrf'; // force CSRF - $before = $filters->globals['before']; - - $Value = csrf_hash(); - $Name = csrf_token(); - $expected = << - - - -EOH; - - $attributes = [ - 'name' => 'form', - 'id' => 'form', - 'method' => 'POST' - ]; - $hidden = [ - 'foo' => 'bar' - ]; - $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); - } - +//FIXME This needs dynamic filters to complete +// public function testFormOpenWithCSRF() +// { +// $config = new App(); +// $config->baseURL = ''; +// $config->indexPage = 'index.php'; +// $request = Services::request($config); +// $request->uri = new URI('http://example.com/'); +// +// Services::injectMock('request', $request); +// +// $filters = Services::filters(); +// $filters->globals['before'][] = 'csrf'; // force CSRF +// $before = $filters->globals['before']; +// +// $Value = csrf_hash(); +// $Name = csrf_token(); +// $expected = << +// +// +// +//EOH; +// +// $attributes = [ +// 'name' => 'form', +// 'id' => 'form', +// 'method' => 'POST' +// ]; +// $hidden = [ +// 'foo' => 'bar' +// ]; +// $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); +// } // ------------------------------------------------------------------------ public function testFormOpenMultipart() { @@ -242,6 +242,10 @@ public function testFormOpenMultipart() 'method' => 'POST' ]; $this->assertEquals($expected, form_open_multipart('foo/bar', $attributes)); + + // make sure it works with attributes as a string too + $attributesString = 'name="form" id="form" method="POST"'; + $this->assertEquals($expected, form_open_multipart('foo/bar', $attributesString)); } // ------------------------------------------------------------------------ @@ -300,7 +304,7 @@ public function testFormInput() } // ------------------------------------------------------------------------ - public function test_form_password() + public function testFormPassword() { $expected = <<\n @@ -309,7 +313,7 @@ public function test_form_password() } // ------------------------------------------------------------------------ - public function test_form_upload() + public function testFormUpload() { $expected = <<\n @@ -318,7 +322,7 @@ public function test_form_upload() } // ------------------------------------------------------------------------ - public function test_form_textarea() + public function testFormTextarea() { $expected = <<Notes\n @@ -341,7 +345,7 @@ public function testFormTextareaWithValueAttribute() } // ------------------------------------------------------------------------ - public function test_form_dropdown() + public function testFormDropdown() { $expected = << @@ -393,6 +397,62 @@ public function test_form_dropdown() $this->assertEquals($expected, form_dropdown('cars', $options, ['volvo', 'audi'])); } + public function testFormDropdownUnselected() + { + $options = [ + 'Swedish Cars' => [ + 'volvo' => 'Volvo', + 'saab' => 'Saab' + ], + 'German Cars' => [ + 'mercedes' => 'Mercedes', + 'audi' => 'Audi' + ] + ]; + $expected = << + + + + + + + + +\n +EOH; + $this->assertEquals($expected, form_dropdown('cars', $options, [])); + } + + public function testFormDropdownInferred() + { + $options = [ + 'Swedish Cars' => [ + 'volvo' => 'Volvo', + 'saab' => 'Saab' + ], + 'German Cars' => [ + 'mercedes' => 'Mercedes', + 'audi' => 'Audi' + ] + ]; + $expected = << + + + + + + + + +\n +EOH; + $_POST['cars'] = 'audi'; + $this->assertEquals($expected, form_dropdown('cars', $options, [])); + unset($_POST['cars']); + } + // ------------------------------------------------------------------------ public function testFormDropdownWithSelectedAttribute() { @@ -445,7 +505,7 @@ public function testFormDropdownWithEmptyArrayOptionValue() } // ------------------------------------------------------------------------ - public function test_form_multiselect() + public function testFormMultiselect() { $expected = << @@ -465,7 +525,7 @@ public function test_form_multiselect() } // ------------------------------------------------------------------------ - public function test_form_fieldset() + public function testFormFieldset() { $expected = << @@ -499,7 +559,7 @@ public function testFormFieldsetWithAttributes() } // ------------------------------------------------------------------------ - public function test_form_fieldset_close() + public function testFormFieldsetClose() { $expected = << @@ -508,7 +568,7 @@ public function test_form_fieldset_close() } // ------------------------------------------------------------------------ - public function test_form_checkbox() + public function testFormCheckbox() { $expected = <<\n @@ -547,7 +607,7 @@ public function testFormCheckboxArrayDataWithCheckedFalse() } // ------------------------------------------------------------------------ - public function test_form_radio() + public function testFormRadio() { $expected = <<\n @@ -556,7 +616,7 @@ public function test_form_radio() } // ------------------------------------------------------------------------ - public function test_form_submit() + public function testFormSubmit() { $expected = <<\n @@ -565,7 +625,7 @@ public function test_form_submit() } // ------------------------------------------------------------------------ - public function test_form_label() + public function testFormLabel() { $expected = <<What is your Name @@ -586,7 +646,7 @@ public function testFormLabelWithAttributes() } // ------------------------------------------------------------------------ - public function test_form_reset() + public function testFormReset() { $expected = <<\n @@ -595,7 +655,7 @@ public function test_form_reset() } // ------------------------------------------------------------------------ - public function test_form_button() + public function testFormButton() { $expected = <<content\n @@ -618,7 +678,7 @@ public function testFormButtonWithDataArray() } // ------------------------------------------------------------------------ - public function test_form_close() + public function testFormClose() { $expected = << @@ -645,7 +705,7 @@ public function testFormDatalist() } // ------------------------------------------------------------------------ - public function test_set_value() + public function testSetValue() { $_SESSION['_ci_old_input']['post']['foo'] = 'assertEquals('<bar', set_value('foo')); @@ -655,7 +715,7 @@ public function test_set_value() } // ------------------------------------------------------------------------ - public function test_set_select() + public function testSetSelect() { $_SESSION['_ci_old_input']['post']['foo'] = 'bar'; $this->assertEquals(' selected="selected"', set_select('foo', 'bar')); @@ -669,7 +729,7 @@ public function test_set_select() } // ------------------------------------------------------------------------ - public function test_set_checkbox() + public function testSetCheckbox() { $_SESSION = [ '_ci_old_input' => [ @@ -696,7 +756,7 @@ public function test_set_checkbox() } // ------------------------------------------------------------------------ - public function test_set_radio() + public function testSetRadio() { $_SESSION = [ '_ci_old_input' => [ @@ -708,6 +768,33 @@ public function test_set_radio() $this->assertEquals(' checked="checked"', set_radio('foo', 'bar')); $this->assertEquals('', set_radio('foo', 'baz')); + unset($_SESSION['_ci_old_input']); + } + + public function testSetRadioFromPost() + { + $_POST['bar'] = 'baz'; + $this->assertEquals(' checked="checked"', set_radio('bar', 'baz')); + $this->assertEquals('', set_radio('bar', 'boop')); + } + + public function testSetRadioFromPostArray() + { + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'bar' => ['boop', 'fuzzy'] + ] + ] + ]; + $this->assertEquals(' checked="checked"', set_radio('bar', 'boop')); + $this->assertEquals('', set_radio('bar', 'baz')); + } + + public function testSetRadioDefault() + { + $this->assertEquals(' checked="checked"', set_radio('code', 'alpha', true)); + $this->assertEquals('', set_radio('code', 'beta', false)); } } diff --git a/user_guide_src/source/helpers/form_helper.rst b/user_guide_src/source/helpers/form_helper.rst index 4c3b6bf38e46..09ecc72810c2 100644 --- a/user_guide_src/source/helpers/form_helper.rst +++ b/user_guide_src/source/helpers/form_helper.rst @@ -40,9 +40,10 @@ characters so that it can be used safely:: .. note:: If you use any of the form helper functions listed on this page, + and you pass values as an associative array, the form values will be automatically escaped, so there is no need to call this function. Use it only if you are creating your own - form elements. + form elements, which you would pass as strings. Available Functions =================== @@ -52,7 +53,7 @@ The following functions are available: .. php:function:: form_open([$action = ''[, $attributes = ''[, $hidden = array()]]]) :param string $action: Form action/target URI string - :param array $attributes: HTML attributes + :param mixed $attributes: HTML attributes, as an array or escaped string :param array $hidden: An array of hidden fields' definitions :returns: An HTML form opening tag :rtype: string @@ -106,15 +107,15 @@ The following functions are available: -.. php:function:: form_open_multipart([$action = ''[, $attributes = array()[, $hidden = array()]]]) +.. php:function:: form_open_multipart([$action = ''[, $attributes = ''[, $hidden = array()]]]) :param string $action: Form action/target URI string - :param array $attributes: HTML attributes + :param mixed $attributes: HTML attributes, as an array or escaped string :param array $hidden: An array of hidden fields' definitions :returns: An HTML multipart form opening tag :rtype: string - This function is absolutely identical to :php:func:`form_open()` above, + This function is identical to :php:func:`form_open()` above, except that it adds a *multipart* attribute, which is necessary if you would like to use the form to upload files with. @@ -289,7 +290,7 @@ The following functions are available: contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple - items through the third parameter, and CodeIgniter will create a + items through the third parameter, and the helper will create a multiple select for you. Example:: From 97bc23d8306a6b3e57f7c923f6b19bc433846763 Mon Sep 17 00:00:00 2001 From: Christoph Potas Date: Fri, 29 Jun 2018 08:55:18 +0200 Subject: [PATCH 07/69] ~ remove lower case access Signed-off-by: Christoph Potas --- system/Config/Config.php | 2 +- tests/system/Config/ConfigTest.php | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/system/Config/Config.php b/system/Config/Config.php index 0cf79dd3e744..0a3c0c204f2b 100644 --- a/system/Config/Config.php +++ b/system/Config/Config.php @@ -70,7 +70,7 @@ public static function get(string $name, bool $getShared = true) $class = substr($name, $pos + 1); } - $class = strtolower($class); + //$class = strtolower($class); if (! $getShared) { diff --git a/tests/system/Config/ConfigTest.php b/tests/system/Config/ConfigTest.php index b1e8397fffe4..ea08b26e981f 100644 --- a/tests/system/Config/ConfigTest.php +++ b/tests/system/Config/ConfigTest.php @@ -7,12 +7,10 @@ class ConfigTest extends \CIUnitTestCase public function testCreateSingleInstance() { - $Config = Config::get('email', false); - $UpperConfig = Config::get('Email', false); + $Config = Config::get('Email', false); $NamespaceConfig = Config::get('Config\\Email', false); $this->assertInstanceOf(Email::class, $Config); - $this->assertInstanceOf(Email::class, $UpperConfig); $this->assertInstanceOf(Email::class, $NamespaceConfig); } @@ -25,7 +23,7 @@ public function testCreateInvalidInstance() public function testCreateSharedInstance() { - $Config = Config::get('email' ); + $Config = Config::get('Email' ); $Config2 = Config::get('Config\\Email'); $this->assertTrue($Config === $Config2); @@ -33,7 +31,7 @@ public function testCreateSharedInstance() public function testCreateNonConfig() { - $Config = Config::get('constants', false); + $Config = Config::get('Constants', false); $this->assertNull($Config); } From 7a07b531e538df439a8130412dd5802c44be1876 Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Fri, 29 Jun 2018 12:59:46 -0700 Subject: [PATCH 08/69] Flesh out html_helper testing --- application/Config/DocTypes.php | 2 +- system/Helpers/html_helper.php | 266 +++++--------- tests/system/Helpers/HTMLHelperTest.php | 347 +++++++++++++----- user_guide_src/source/helpers/html_helper.rst | 22 +- 4 files changed, 355 insertions(+), 282 deletions(-) diff --git a/application/Config/DocTypes.php b/application/Config/DocTypes.php index 37830984c31f..38ceaedfc905 100755 --- a/application/Config/DocTypes.php +++ b/application/Config/DocTypes.php @@ -8,7 +8,7 @@ class DocTypes { - static $list = + public $list = [ 'xhtml11' => '', 'xhtml1-strict' => '', diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index ac8d624c9abc..aa8ae6c9434a 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -97,22 +97,13 @@ function ol(array $list, string $attributes = ''): string * Generates an HTML ordered list from an single or multi-dimensional array. * * @param string $type - * @param array $list + * @param mixed $list * @param string $attributes * @param int $depth * @return string */ - function _list - ( - string $type = 'ul', array $list = [], string $attributes = '', int $depth = 0 - ): string + function _list(string $type = 'ul', $list = [], string $attributes = '', int $depth = 0): string { - // If an array wasn't submitted there's nothing to do... - if ( ! is_array($list)) - { - return $list; - } - // Set the indentation based on the depth $out = str_repeat(' ', $depth) // Write the opening list tag @@ -162,20 +153,16 @@ function _list * * @param mixed $src * @param bool $indexPage - * @param string $attributes + * @param mixed $attributes * @return string */ - function img - ( - $src = '', bool $indexPage = false, string $attributes = '' - ): string + function img($src = '', bool $indexPage = false, $attributes = ''): string { if ( ! is_array($src)) { $src = ['src' => $src]; } - //If there is no alt attribute defined, set it to an empty string. if ( ! isset($src['alt'])) { @@ -223,20 +210,13 @@ function img * xhtml-frame, html4-strict, html4-trans, and html4-frame. * All values are saved in the doctypes config file. * - * @param mixed $type The doctype to be generated + * @param string $type The doctype to be generated * @return string */ - function doctype($type = 'html5'): string + function doctype(string $type = 'html5'): string { - $doctypes = null; - $env = ENVIRONMENT; - $doctypes = Config\DocTypes::$list; - $customDocTypesPath = APPPATH . "Config/{$env}/DocTypes.php"; - if (file_exists($customDocTypesPath)) - { - $customDocTypesNs = "Config\{$env}\DocTypes"; - $doctypes = $customDocTypesNs::$list; - } + $config = new \Config\DocTypes(); + $doctypes = $config->list; return $doctypes[$type] ?? false; } @@ -256,47 +236,30 @@ function doctype($type = 'html5'): string * @param bool $indexPage Should indexPage be added to the JS path * @return string */ - function script_tag - ( - $src = '', bool $indexPage = false - ): string + function script_tag($src = '', bool $indexPage = false): string { $script = '', - script_tag('http://site.com/js/mystyles.js') - ); + $target = 'http://site.com/js/mystyles.js'; + $expected = ''; + $this->assertEquals($expected, script_tag($target)); + } + + public function testScriptTagWithoutProtocol() + { + $target = 'js/mystyles.js'; + $expected = ''; + $this->assertEquals($expected, script_tag($target)); + } + + public function testScriptTagWithIndexpage() + { + $target = 'js/mystyles.js'; + $expected = ''; + $this->assertEquals($expected, script_tag($target, true)); } // ------------------------------------------------------------------------ public function testLinkTag() { - $this->assertEquals - ( - '', - link_tag('http://site.com/css/mystyles.css') - ); + $target = 'css/mystyles.css'; + $expected = ''; + $this->assertEquals($expected, link_tag($target)); + } + + public function testLinkTagComplete() + { + $target = 'https://styles.com/css/mystyles.css'; + $expected = ''; + $this->assertEquals($expected, link_tag($target, 'banana', 'fruit', 'Go away', 'VHS')); + } + + public function testLinkTagArray() + { + $parms = [ + 'href' => 'css/mystyles.css', + 'indexPage' => true, + ]; + $expected = ''; + $this->assertEquals($expected, link_tag($parms)); } // ------------------------------------------------------------------------ public function testDocType() { - $this->assertEquals - ( - '', - doctype('html4-strict') - ); + $target = 'html4-strict'; + $expected = ''; + $this->assertEquals($expected, doctype($target)); + } + + public function testDocTypeDefault() + { + $expected = ''; + $this->assertEquals($expected, doctype()); + } + + public function testDocTypeInvalid() + { + $target = 'good-guess'; + $this->assertEquals(false, doctype($target)); } // ------------------------------------------------------------------------ @@ -182,23 +229,39 @@ public function testDocType() public function testVideo() { $expected = << + EOH; - $video = video - ( - 'test.mp4', - 'Your browser does not support the video tag.', - 'controls' - ); + $target = 'http://www.codeigniter.com/test.mp4'; + $message = 'Your browser does not support the video tag.'; + $video = video($target, $message, 'controls'); + $this->assertEquals($expected, $video); + } + public function testVideoWithTracks() + { + $expected = << + + + Your browser does not support the video tag. + + +EOH; + + $target = 'test.mp4'; + $message = 'Your browser does not support the video tag.'; + $video = video($target, $message, 'controls', $this->tracks); $this->assertEquals($expected, $video); + } + public function testVideoWithTracksAndIndex() + { $expected = << +