Skip to content

Commit

Permalink
Merge pull request #289 from FriendsOfCake/improvements-and-fixes
Browse files Browse the repository at this point in the history
Improvements and fixes
  • Loading branch information
jippi committed Jun 26, 2015
2 parents d9fae9c + feca39b commit 5babdb5
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Action/IndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class IndexAction extends BaseAction
*/
protected function _handle()
{
$subject = $this->_subject(['success' => true, 'query' => null]);
$query = $this->_table()->find();
$subject = $this->_subject(['success' => true, 'query' => $query]);

$this->_trigger('beforePaginate', $subject);
$items = $this->_controller()->paginate($subject->query);
Expand Down
8 changes: 4 additions & 4 deletions src/Action/LookupAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class LookupAction extends BaseAction
*/
protected function _handle()
{
$subject = $this->_subject(['success' => true]);
$query = $this->_table()->find($this->config('findMethod'), $this->_getFindConfig());
$subject = $this->_subject(['success' => true, 'query' => $query]);

$this->_trigger('beforeLookup', $subject);
$query = $this->_table()->find($this->config('findMethod'), $this->_getFindConfig());
$subject->set(['entities' => $this->_controller()->paginate($query)]);
$subject->set(['entities' => $this->_controller()->paginate($subject->query)]);
$this->_trigger('afterLookup', $subject);

$this->_trigger('beforeRender', $subject);
Expand All @@ -57,7 +57,7 @@ protected function _getFindConfig()

$config = (array)$this->config('findConfig');
if ($idField = $request->query('id')) {
$config['idField'] = $idField;
$config['keyField'] = $idField;
}

if ($valueField = $request->query('value')) {
Expand Down
1 change: 1 addition & 0 deletions tests/App/Controller/BlogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BlogsController extends Controller
'Crud.Edit',
'Crud.View',
'Crud.Delete',
'Crud.Lookup',
'deleteAll' => [
'className' => 'Crud.Bulk/Delete',
],
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Action/IndexActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,25 @@ function ($event) {
$this->assertNotNull($this->viewVariable('items'));
$this->assertNotNull($this->viewVariable('success'));
}

/**
* Tests that it is posisble to modify the pagination query in beforePaginate
*
* @return void
*/
public function testModifyQueryInEvent()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_controller->Crud->on('beforePaginate', function ($event) {
$event->subject->query->where(['id <' => 2]);
});
}
);

$this->get('/blogs');
$this->assertContains('Page 1 of 1, showing 1 records out of 1 total', $this->_response->body());
}
}
107 changes: 107 additions & 0 deletions tests/TestCase/Action/LookupActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
namespace Crud\Test\TestCase\Action;

use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Crud\TestSuite\IntegrationTestCase;

/**
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
*/
class LookupActionTest extends IntegrationTestCase
{

/**
* fixtures property
*
* @var array
*/
public $fixtures = ['plugin.crud.blogs'];

/**
* Test with no extra options
*
* @return void
*/
public function testGet()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_subscribeToEvents($this->_controller);
}
);

$expected = [
'1' => '1st post',
'2' => '2nd post',
'3' => '3rd post',
];

$this->get('/blogs/lookup.json');
$this->assertEvents(['beforeLookup', 'afterLookup', 'beforeRender']);
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertNotNull($this->viewVariable('blogs'));
$this->assertNotNull($this->viewVariable('success'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}

/**
* Test changing the id field and value field
*
* @return void
*/
public function testGetWithCustomParams()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_subscribeToEvents($this->_controller);
}
);

$expected = [
'1st post' => '1',
'2nd post' => '2',
'3rd post' => '3',
];

$this->get('/blogs/lookup.json?id=name&value=id');
$this->assertEvents(['beforeLookup', 'afterLookup', 'beforeRender']);
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertNotNull($this->viewVariable('blogs'));
$this->assertNotNull($this->viewVariable('success'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}


/**
* Tests that the beforeLookup can be used to modify the query
*
* @return void
*/
public function testGetWithQueryModification()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_controller->Crud->on('beforeLookup', function ($event) {
$event->subject->query->where(['id <' => 2]);
});
}
);

$expected = [
'1' => '1st post',
];

$this->get('/blogs/lookup.json');
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}
}

0 comments on commit 5babdb5

Please sign in to comment.