Skip to content

Commit

Permalink
Merge pull request #311 from FriendsOfCake/issue-310
Browse files Browse the repository at this point in the history
Don't override user provided "data-url".
  • Loading branch information
ADmad authored Jan 8, 2023
2 parents fad8ab9 + f6a7878 commit 7d50bde
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Fixture/BlogsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace CrudView\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class BlogsFixture extends TestFixture
{
public $fields = [
'id' => ['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'],
];
}
73 changes: 73 additions & 0 deletions tests/TestCase/Listener/ViewSearchListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);

namespace CrudView\Test\TestCase\Listener;

use Cake\Controller\Controller;
use Cake\Http\ServerRequest;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use CrudView\Listener\ViewSearchListener;

/**
* Test case for ViewSearchListener.
*/
class ViewSearchListenerTest extends TestCase
{
protected $fixtures = ['plugin.CrudView.Blogs'];

/**
* @var \Cake\Controller\Controller;
*/
protected $controller;

/**
* @var \CrudView\Listener\ViewSearchListener
*/
protected $listener;

public function setUp(): void
{
$routesBuilder = Router::createRouteBuilder('/');
$routesBuilder->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);
}
}

0 comments on commit 7d50bde

Please sign in to comment.