Skip to content

Commit 7d50bde

Browse files
authored
Merge pull request #311 from FriendsOfCake/issue-310
Don't override user provided "data-url".
2 parents fad8ab9 + f6a7878 commit 7d50bde

File tree

3 files changed

+108
-8
lines changed

3 files changed

+108
-8
lines changed

src/Listener/ViewSearchListener.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,20 @@ public function fields(): array
153153
];
154154
}
155155

156-
$urlArgs = [];
157-
158-
$fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field];
159-
if (is_array($fieldKeys)) {
160-
foreach ($fieldKeys as $key => $val) {
161-
$urlArgs[$key] = $val;
156+
if (!isset($input['data-url'])) {
157+
$urlArgs = [];
158+
159+
$fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field];
160+
if (is_array($fieldKeys)) {
161+
foreach ($fieldKeys as $key => $val) {
162+
$urlArgs[$key] = $val;
163+
}
162164
}
165+
166+
$input['data-url'] = Router::url(['action' => 'lookup', '_ext' => 'json', '?' => $urlArgs]);
163167
}
164168

165169
unset($input['fields']);
166-
$url = array_merge(['action' => 'lookup', '_ext' => 'json'], ['?' => $urlArgs]);
167-
$input['data-url'] = Router::url($url);
168170

169171
$fields[$field] = $input;
170172
}

tests/Fixture/BlogsFixture.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace CrudView\Test\Fixture;
5+
6+
use Cake\TestSuite\Fixture\TestFixture;
7+
8+
class BlogsFixture extends TestFixture
9+
{
10+
public $fields = [
11+
'id' => ['type' => 'integer'],
12+
'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false],
13+
'name' => ['type' => 'string', 'length' => 255, 'null' => false],
14+
'body' => ['type' => 'text', 'null' => false],
15+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
16+
];
17+
18+
public $records = [
19+
['name' => '1st post', 'body' => '1st post body'],
20+
['name' => '2nd post', 'body' => '2nd post body'],
21+
['name' => '3rd post', 'body' => '3rd post body'],
22+
['name' => '4th post', 'body' => '4th post body'],
23+
['name' => '5th post', 'body' => '5th post body'],
24+
];
25+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace CrudView\Test\TestCase\Listener;
5+
6+
use Cake\Controller\Controller;
7+
use Cake\Http\ServerRequest;
8+
use Cake\Routing\Route\DashedRoute;
9+
use Cake\Routing\Router;
10+
use Cake\TestSuite\TestCase;
11+
use CrudView\Listener\ViewSearchListener;
12+
13+
/**
14+
* Test case for ViewSearchListener.
15+
*/
16+
class ViewSearchListenerTest extends TestCase
17+
{
18+
protected $fixtures = ['plugin.CrudView.Blogs'];
19+
20+
/**
21+
* @var \Cake\Controller\Controller;
22+
*/
23+
protected $controller;
24+
25+
/**
26+
* @var \CrudView\Listener\ViewSearchListener
27+
*/
28+
protected $listener;
29+
30+
public function setUp(): void
31+
{
32+
$routesBuilder = Router::createRouteBuilder('/');
33+
$routesBuilder->setRouteClass(DashedRoute::class);
34+
$routesBuilder->connect('/{controller}/{action}/*', [])
35+
->setExtensions(['json']);
36+
37+
$request = new ServerRequest([
38+
'url' => '/blogs/index',
39+
'params' => ['controller' => 'Blogs', 'action' => 'index', 'plugin' => null, '_ext' => null],
40+
]);
41+
42+
$this->controller = new Controller($request, null, 'Blogs');
43+
44+
$this->listener = new ViewSearchListener($this->controller);
45+
46+
Router::setRequest($request);
47+
}
48+
49+
public function testFields()
50+
{
51+
$this->listener->setConfig(['fields' => ['category_id']]);
52+
53+
$fields = $this->listener->fields();
54+
$expected = [
55+
'category_id' => [
56+
'required' => false,
57+
'type' => 'select',
58+
'value' => null,
59+
'class' => 'autocomplete',
60+
'data-url' => '/blogs/lookup.json?id=category_id&value=category_id',
61+
],
62+
];
63+
$this->assertEquals($expected, $fields);
64+
65+
$this->listener->setConfig([
66+
'fields' => ['category_id' => ['data-url' => '/custom']],
67+
], null, true);
68+
69+
$fields = $this->listener->fields();
70+
$expected['category_id']['data-url'] = '/custom';
71+
$this->assertEquals($expected, $fields);
72+
}
73+
}

0 commit comments

Comments
 (0)