From f6a7878aa4380a34ecfcd8da1e58f7605fabeef8 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 7 Jan 2023 18:45:52 +0530 Subject: [PATCH] Don't override user provided "data-url". Closes #310 --- src/Listener/ViewSearchListener.php | 18 +++-- tests/Fixture/BlogsFixture.php | 25 +++++++ .../Listener/ViewSearchListenerTest.php | 73 +++++++++++++++++++ 3 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 tests/Fixture/BlogsFixture.php create mode 100644 tests/TestCase/Listener/ViewSearchListenerTest.php diff --git a/src/Listener/ViewSearchListener.php b/src/Listener/ViewSearchListener.php index b53270c..e46ba0e 100644 --- a/src/Listener/ViewSearchListener.php +++ b/src/Listener/ViewSearchListener.php @@ -153,18 +153,20 @@ public function fields(): array ]; } - $urlArgs = []; - - $fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field]; - if (is_array($fieldKeys)) { - foreach ($fieldKeys as $key => $val) { - $urlArgs[$key] = $val; + if (!isset($input['data-url'])) { + $urlArgs = []; + + $fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field]; + if (is_array($fieldKeys)) { + foreach ($fieldKeys as $key => $val) { + $urlArgs[$key] = $val; + } } + + $input['data-url'] = Router::url(['action' => 'lookup', '_ext' => 'json', '?' => $urlArgs]); } unset($input['fields']); - $url = array_merge(['action' => 'lookup', '_ext' => 'json'], ['?' => $urlArgs]); - $input['data-url'] = Router::url($url); $fields[$field] = $input; } diff --git a/tests/Fixture/BlogsFixture.php b/tests/Fixture/BlogsFixture.php new file mode 100644 index 0000000..4a677fb --- /dev/null +++ b/tests/Fixture/BlogsFixture.php @@ -0,0 +1,25 @@ + ['type' => 'integer'], + 'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false], + 'name' => ['type' => 'string', 'length' => 255, 'null' => false], + 'body' => ['type' => 'text', 'null' => false], + '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], + ]; + + public $records = [ + ['name' => '1st post', 'body' => '1st post body'], + ['name' => '2nd post', 'body' => '2nd post body'], + ['name' => '3rd post', 'body' => '3rd post body'], + ['name' => '4th post', 'body' => '4th post body'], + ['name' => '5th post', 'body' => '5th post body'], + ]; +} diff --git a/tests/TestCase/Listener/ViewSearchListenerTest.php b/tests/TestCase/Listener/ViewSearchListenerTest.php new file mode 100644 index 0000000..696cd72 --- /dev/null +++ b/tests/TestCase/Listener/ViewSearchListenerTest.php @@ -0,0 +1,73 @@ +setRouteClass(DashedRoute::class); + $routesBuilder->connect('/{controller}/{action}/*', []) + ->setExtensions(['json']); + + $request = new ServerRequest([ + 'url' => '/blogs/index', + 'params' => ['controller' => 'Blogs', 'action' => 'index', 'plugin' => null, '_ext' => null], + ]); + + $this->controller = new Controller($request, null, 'Blogs'); + + $this->listener = new ViewSearchListener($this->controller); + + Router::setRequest($request); + } + + public function testFields() + { + $this->listener->setConfig(['fields' => ['category_id']]); + + $fields = $this->listener->fields(); + $expected = [ + 'category_id' => [ + 'required' => false, + 'type' => 'select', + 'value' => null, + 'class' => 'autocomplete', + 'data-url' => '/blogs/lookup.json?id=category_id&value=category_id', + ], + ]; + $this->assertEquals($expected, $fields); + + $this->listener->setConfig([ + 'fields' => ['category_id' => ['data-url' => '/custom']], + ], null, true); + + $fields = $this->listener->fields(); + $expected['category_id']['data-url'] = '/custom'; + $this->assertEquals($expected, $fields); + } +}