Skip to content

Commit

Permalink
Merge pull request #1513 from fquffio/issue/v4/1484-order-contents-in…
Browse files Browse the repository at this point in the history
…-folder

#1484 Sort contents in folder
  • Loading branch information
stefanorosanelli committed Jul 18, 2018
2 parents 90f7188 + b180827 commit 103fed4
Show file tree
Hide file tree
Showing 12 changed files with 812 additions and 19 deletions.
13 changes: 10 additions & 3 deletions plugins/BEdita/API/src/Error/ExceptionRenderer.php
Expand Up @@ -116,9 +116,16 @@ protected function errorDetail(\Exception $error)
$res = '';
if (is_array($d)) {
$d = Hash::flatten($d);
foreach ($d as $item => $errDetail) {
$res .= "[$item]: $errDetail ";
}
$res = implode(
' ',
array_map(
function ($key, $val) {
return sprintf('[%s]: %s', $key, $val);
},
array_keys($d),
array_values($d)
)
);
}

return $res;
Expand Down
2 changes: 1 addition & 1 deletion plugins/BEdita/API/tests/IntegrationTest/I18nTest.php
Expand Up @@ -58,7 +58,7 @@ public function testWrongLang()
'error' => [
'status' => '400',
'title' => 'Invalid data',
'detail' => '[lang.languageTag]: Invalid language tag "fi" ',
'detail' => '[lang.languageTag]: Invalid language tag "fi"',
],
];

Expand Down
Expand Up @@ -10,7 +10,7 @@
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\Api\Test\IntegrationTest;
namespace BEdita\API\Test\IntegrationTest;

use BEdita\API\TestSuite\IntegrationTestCase;
use Cake\ORM\TableRegistry;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function tearDown()
}

/**
* Undocumented function
* Test setting a parent to an object.
*
* @return void
*/
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testParents()
sort($parentIds);
static::assertEquals(Hash::extract($data, '{n}.id'), $parentIds);

// DELETE: delete all remining parents relationships
// DELETE: delete all remaining parents relationships
$this->configRequestHeaders('DELETE', $authHeader);
// Cannot use `IntegrationTestCase::delete()`, as it does not allow sending payload with the request.
$this->_sendRequest($relationshipsEndpoint, 'DELETE', json_encode(compact('data')));
Expand Down Expand Up @@ -219,4 +219,97 @@ public function testIncludeParents()
$includedIds = Hash::extract($result, 'included.{n}.id');
static::assertEquals(['11'], $includedIds);
}

/**
* Test setting an object's parent with position.
*
* @return void
*
* @coversNothing
*/
public function testSetParentPosition()
{
$this->configRequestHeaders('POST', $this->getUserAuthHeader());
$data = [
[
'id' => '12',
'type' => 'folders',
'meta' => [
'relation' => [
'position' => 1,
],
],
],
];
$this->post('/documents/2/relationships/parents', json_encode(compact('data')));
$this->assertResponseCode(200);

$childrenIds = $this->Trees->find('list', ['valueField' => 'object_id'])
->where(['parent_id' => 12])
->order(['tree_left' => 'ASC'])
->toList();

static::assertEquals([2, 4], $childrenIds);
}

/**
* Data provider for `testSetParentPositionInvalid` test case.
*
* @return array
*/
public function setParentPositionInvalidProvider()
{
return [
'zero' => [
'[position.notEquals]: The provided value is invalid',
0,
],
'invalid string' => [
'[position.inList]: The provided value is invalid',
'gustavo',
],
'empty' => [
'[position._empty]: This field cannot be left empty',
'',
],
];
}

/**
* Test setting an object's parent with an invalid position.
*
* @param string $expected Expected error.
* @param int|string $position Desired position.
* @return void
*
* @dataProvider setParentPositionInvalidProvider()
* @coversNothing
*/
public function testSetParentPositionInvalid($expected, $position)
{
$this->configRequestHeaders('POST', $this->getUserAuthHeader());
$data = [
[
'id' => '12',
'type' => 'folders',
'meta' => [
'relation' => compact('position'),
],
],
];
$this->post('/documents/2/relationships/parents', json_encode(compact('data')));
$this->assertResponseCode(400);

$result = json_decode((string)$this->_response->getBody(), true);

static::assertEquals('Invalid data', $result['error']['title']);
static::assertEquals($expected, $result['error']['detail']);

$childrenIds = $this->Trees->find('list', ['valueField' => 'object_id'])
->where(['parent_id' => 12])
->order(['tree_left' => 'ASC'])
->toList();

static::assertEquals([4], $childrenIds);
}
}
126 changes: 126 additions & 0 deletions plugins/BEdita/API/tests/TestCase/Controller/FoldersControllerTest.php
Expand Up @@ -22,6 +22,34 @@
*/
class FoldersControllerTest extends IntegrationTestCase
{

/**
* Folders table.
*
* @var \BEdita\Core\Model\Table\FoldersTable+
*/
public $Folders;

/**
* {@inheritDoc}
*/
public function setUp()
{
parent::setUp();

$this->Folders = TableRegistry::get('Folders');
}

/**
* {@inheritDoc}
*/
public function tearDown()
{
unset($this->Folders);

parent::tearDown();
}

/**
* Test index method.
*
Expand Down Expand Up @@ -870,4 +898,102 @@ public function testMoveFolder($folderId, $parentId)
static::assertSame((string)$parentId, Hash::get($result, 'data.id'));
}
}

/**
* Test setting a folder's children with position.
*
* @return void
*
* @coversNothing
*/
public function testSetChildrenPosition()
{
$this->configRequestHeaders('POST', $this->getUserAuthHeader());
$data = [
[
'id' => '2',
'type' => 'documents',
'meta' => [
'relation' => [
'position' => 3,
],
],
],
[
'id' => '5',
'type' => 'users',
'meta' => [
'relation' => [
'position' => 'first',
],
],
],
];
$this->post('/folders/12/relationships/children', json_encode(compact('data')));
$this->assertResponseCode(200);

$folder = $this->Folders->get(12, ['contain' => ['Children']]);

$childrenIds = Hash::extract($folder->children, '{n}.id');
static::assertEquals(['5', '4', '2'], $childrenIds);
}

/**
* Data provider for `testSetChildrenPositionInvalid` test case.
*
* @return array
*/
public function setChildrenPositionInvalidProvider()
{
return [
'zero' => [
'[position.notEquals]: The provided value is invalid',
0,
],
'invalid string' => [
'[position.inList]: The provided value is invalid',
'gustavo',
],
'empty' => [
'[position._empty]: This field cannot be left empty',
'',
],
];
}

/**
* Test setting a folder's children with an invalid position.
*
* @param string $expected Expected error.
* @param int|string $position Desired position.
* @return void
*
* @dataProvider setChildrenPositionInvalidProvider()
* @coversNothing
*/
public function testSetChildrenPositionInvalid($expected, $position)
{
$this->configRequestHeaders('POST', $this->getUserAuthHeader());
$data = [
[
'id' => '2',
'type' => 'documents',
'meta' => [
'relation' => compact('position'),
],
],
];
$this->post('/folders/12/relationships/children', json_encode(compact('data')));
$this->assertResponseCode(400);

$result = json_decode((string)$this->_response->getBody(), true);

static::assertEquals('Invalid data', $result['error']['title']);
static::assertEquals($expected, $result['error']['detail']);

$folder = $this->Folders->get(12, ['contain' => ['Children']]);

$childrenIds = Hash::extract($folder->children, '{n}.id');
static::assertEquals(['4'], $childrenIds);
}
}
Expand Up @@ -83,7 +83,7 @@ public function errorDetailsProvider()
'detailArray' => [
['title' => 'new title', 'detail' => [['field' => ['cause' => 'err detail']]]],
'new title',
'[0.field.cause]: err detail '
'[0.field.cause]: err detail'
],
'detailArray2' => [
[
Expand All @@ -97,7 +97,7 @@ public function errorDetailsProvider()
]
],
'new title',
'[field.cause]: err detail [nestedFields.field2.cause2]: err detail2 [nestedFields.field3.cause3]: err detail3 '
'[field.cause]: err detail [nestedFields.field2.cause2]: err detail2 [nestedFields.field3.cause3]: err detail3'
],
'code' => [
['title' => 'err title', 'code' => 'err-code'],
Expand Down

0 comments on commit 103fed4

Please sign in to comment.