mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-27 08:21:03 +02:00
Unique folder title
This commit is contained in:
parent
4b27aba0d8
commit
a62c431723
@ -6,6 +6,7 @@
|
||||
echo '============================================================================================='
|
||||
echo 'Testing controllers....'
|
||||
echo '============================================================================================='
|
||||
#php5.6 phpunit-5.7.20.phar --filter testDuplicateTitle tests/Controller/FoldersControllerTest.php --bootstrap vendor/autoload.php tests/Controller/
|
||||
php5.6 phpunit-5.7.20.phar --bootstrap vendor/autoload.php tests/Controller/
|
||||
|
||||
# echo ""
|
||||
|
@ -24,6 +24,7 @@ class FoldersController extends ApiController {
|
||||
$folder = new Folder();
|
||||
$folder->fromPublicArray($request->request->all());
|
||||
$folder->owner_id = $this->user()->id;
|
||||
$folder->validate();
|
||||
$folder->save();
|
||||
return static::successResponse(Folder::find($folder->id));
|
||||
}
|
||||
@ -43,7 +44,7 @@ class FoldersController extends ApiController {
|
||||
}
|
||||
|
||||
$query = $request->query->all();
|
||||
if ($folder) $folder->revId = $query['rev_id'];
|
||||
if ($folder && isset($query['rev_id'])) $folder->revId = $query['rev_id'];
|
||||
|
||||
if ($request->isMethod('PUT')) {
|
||||
$isNew = !$folder;
|
||||
@ -52,6 +53,7 @@ class FoldersController extends ApiController {
|
||||
$folder->id = Folder::unhex($id);
|
||||
$folder->owner_id = $this->user()->id;
|
||||
$folder->setIsNew($isNew);
|
||||
$folder->validate();
|
||||
$folder->save();
|
||||
return static::successResponse($folder);
|
||||
}
|
||||
@ -60,6 +62,7 @@ class FoldersController extends ApiController {
|
||||
$data = $this->patchParameters();
|
||||
$folder->fromPublicArray(Folder::filter($this->patchParameters()));
|
||||
$folder->id = Folder::unhex($id);
|
||||
$folder->validate();
|
||||
$folder->save();
|
||||
return static::successResponse($folder);
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ class NotesController extends ApiController {
|
||||
}
|
||||
|
||||
$query = $request->query->all();
|
||||
if ($note) $note->revId = $query['rev_id'];
|
||||
if ($note && isset($query['rev_id'])) $note->revId = $query['rev_id'];
|
||||
|
||||
if ($request->isMethod('PUT')) {
|
||||
$isNew = !$note;
|
||||
|
@ -211,6 +211,7 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
|
||||
}
|
||||
|
||||
public function diffableField($fieldName) {
|
||||
if (array_key_exists($fieldName, $this->changedDiffableFields)) return $this->changedDiffableFields[$fieldName];
|
||||
return Change::fullFieldText($this->id, $fieldName);
|
||||
}
|
||||
|
||||
@ -279,6 +280,7 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
|
||||
foreach ($rules as $key => $keyRules) {
|
||||
foreach ($keyRules as $rule) {
|
||||
$ok = true;
|
||||
$message = null;
|
||||
switch ($rule['type']) {
|
||||
|
||||
// Note: Cannot use property_exists() since all the key are retrieved via the magic
|
||||
@ -311,6 +313,10 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
|
||||
case 'function':
|
||||
|
||||
$ok = call_user_func_array($rule['args'][0], array($key, $rule, $this));
|
||||
if (is_array($ok)) {
|
||||
$message = $ok['message'];
|
||||
$ok = $ok['valid'];
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -323,7 +329,7 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
|
||||
$errors[] = array(
|
||||
'key' => $key,
|
||||
'type' => $rule['type'] == 'function' ? 'other' : $rule['type'],
|
||||
'message' => static::validationMessage($key, $rule, $this),
|
||||
'message' => $message ? $message : static::validationMessage($key, $rule, $this),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,13 @@ class Folder extends BaseItem {
|
||||
'is_default' => array('public' => 'bool'),
|
||||
);
|
||||
|
||||
static protected $defaultValidationRules = array(
|
||||
'title' => array(
|
||||
array('type' => 'notEmpty'),
|
||||
array('type' => 'function', 'args' => array(array('AppBundle\Model\Folder', 'validateUniqueTitle'))),
|
||||
),
|
||||
);
|
||||
|
||||
public function add($ids) {
|
||||
$notes = Note::find($ids);
|
||||
foreach ($notes as $note) {
|
||||
@ -64,4 +71,26 @@ class Folder extends BaseItem {
|
||||
return $output;
|
||||
}
|
||||
|
||||
static public function allByOwnerId($ownerId) {
|
||||
return Folder::where('owner_id', '=', $ownerId)->get();
|
||||
}
|
||||
|
||||
static public function byTitle($ownerId, $title) {
|
||||
$folders = static::allByOwnerId($ownerId);
|
||||
foreach ($folders as $folder) {
|
||||
if ($folder->diffableField('title') == $title) return $folder;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static public function validateUniqueTitle($key, $rule, $object) {
|
||||
$title = $object->diffableField('title');
|
||||
$folder = self::byTitle($object->owner_id, $title);
|
||||
if ($folder && $folder->id == $object->id) return true;
|
||||
return array(
|
||||
'valid' => !$folder,
|
||||
'message' => sprintf('title "%s" is already in use', $title),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,25 +12,35 @@ class FoldersControllerTest extends BaseControllerTestCase {
|
||||
Folder::truncate();
|
||||
}
|
||||
|
||||
public function testDefault() {
|
||||
// $this->loadSession(1, 1);
|
||||
public function testDuplicateTitle() {
|
||||
$this->loadSession(1, 1);
|
||||
|
||||
// $f1 = $this->request('POST', '/folders', null, array(
|
||||
// 'title' => 'first folder',
|
||||
// 'is_default' => true,
|
||||
// ));
|
||||
$f1 = $this->request('POST', '/folders', null, array('title' => 'one'));
|
||||
|
||||
// $this->assertArrayHasKey('is_default', $f1);
|
||||
// $this->assertTrue($f1['is_default']);
|
||||
$r = $this->request('POST', '/folders', null, array('title' => 'one'));
|
||||
|
||||
// $f2 = $this->request('POST', '/folders', null, array(
|
||||
// 'title' => 'first folder',
|
||||
// 'is_default' => true,
|
||||
// ));
|
||||
$this->assertArrayHasKey('error', $r);
|
||||
$this->assertEquals('Validation', $r['type']);
|
||||
|
||||
// $f1 = $this->request('GET', '/folders/' . $f1['id']);
|
||||
$r = $this->request('PUT', '/folders/' . Folder::createId(), null, array('title' => 'one'));
|
||||
$this->assertEquals('Validation', $r['type']);
|
||||
|
||||
// $this->assertFalse($f1['is_default']);
|
||||
$f2 = $this->request('POST', '/folders', null, array('title' => 'two'));
|
||||
$r = $this->request('PATCH', '/folders/' . $f2['id'], null, array('title' => 'one'));
|
||||
$this->assertEquals('Validation', $r['type']);
|
||||
}
|
||||
|
||||
public function testNotDuplicateTitle() {
|
||||
$this->loadSession(1, 1);
|
||||
|
||||
$f1 = $this->request('POST', '/folders', null, array('title' => 'one'));
|
||||
$f2 = $this->request('PUT', '/folders/' . $f1['id'], null, array('title' => 'one'));
|
||||
|
||||
$this->assertEquals($f1['id'], $f2['id']);
|
||||
|
||||
$f2 = $this->request('PATCH', '/folders/' . $f1['id'], null, array('title' => 'one'));
|
||||
|
||||
$this->assertEquals($f1['id'], $f2['id']);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user