mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-23 18:53:36 +02:00
Handle default folder
This commit is contained in:
parent
7c43f38be5
commit
fdab917043
3
ReactNativeClient/start_emulator.bat
Normal file
3
ReactNativeClient/start_emulator.bat
Normal file
@ -0,0 +1,3 @@
|
||||
c:
|
||||
cd c:\Users\Laurent\AppData\Local\Android\sdk\tools
|
||||
emulator.exe -avd Nexus_5X_API_23_Google_API_
|
@ -3,5 +3,5 @@
|
||||
# Example to test just one method of a test unit:
|
||||
# php phpunit-5.7.20.phar --filter testConflict ChangeTest tests/Model/ChangeTest.php --bootstrap vendor/autoload.php tests/Model/
|
||||
|
||||
php5.6 phpunit-5.7.20.phar --bootstrap vendor/autoload.php tests/Controller/
|
||||
# php5.6 phpunit-5.7.20.phar --bootstrap vendor/autoload.php tests/Controller/
|
||||
php5.6 phpunit-5.7.20.phar --bootstrap vendor/autoload.php tests/Model/
|
@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace AppBundle;
|
||||
|
||||
class ApiSerializer {
|
||||
|
||||
|
||||
|
||||
}
|
@ -25,7 +25,7 @@ class FoldersController extends ApiController {
|
||||
$folder->fromPublicArray($request->request->all());
|
||||
$folder->owner_id = $this->user()->id;
|
||||
$folder->save();
|
||||
return static::successResponse($folder);
|
||||
return static::successResponse(Folder::find($folder->id));
|
||||
}
|
||||
|
||||
throw new MethodNotAllowedException();
|
||||
|
@ -155,6 +155,25 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
|
||||
}
|
||||
}
|
||||
|
||||
static public function dbValueToPublicValue($fieldName, $fieldValue) {
|
||||
if (static::isDiffableField($fieldName)) return (string)$fieldValue;
|
||||
if (!static::isValidField($fieldName)) throw new \Exception('Unknown field: ' . $fieldName);
|
||||
|
||||
switch (static::$fields[$fieldName]['public']) {
|
||||
case 'string':
|
||||
return (string)$fieldValue;
|
||||
break;
|
||||
case 'int':
|
||||
return (int)$fieldValue;
|
||||
break;
|
||||
case 'bool':
|
||||
return $fieldValue === '1' || $fieldValue === 1 || $fieldValue === true;
|
||||
break;
|
||||
}
|
||||
|
||||
return $fieldValue;
|
||||
}
|
||||
|
||||
public function toPublicArray() {
|
||||
$output = $this->toArray();
|
||||
if ($this->useUuid) {
|
||||
@ -171,6 +190,7 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
|
||||
if (isset(static::$enums[$k])) {
|
||||
$output[$k] = static::enumName($k, $v);
|
||||
}
|
||||
$output[$k] = static::dbValueToPublicValue($k, $v);
|
||||
}
|
||||
|
||||
if (isset($output['item_type'])) {
|
||||
|
@ -9,14 +9,14 @@ class Folder extends BaseItem {
|
||||
static protected $diffableFields = array('title');
|
||||
|
||||
static protected $fields = array(
|
||||
'id' => null,
|
||||
'created_time' => null,
|
||||
'updated_time' => null,
|
||||
'parent_id' => null,
|
||||
'owner_id' => null,
|
||||
'is_encrypted' => null,
|
||||
'encryption_method' => null,
|
||||
'is_default' => null,
|
||||
'id' => array('public' => 'string'),
|
||||
'created_time' => array('public' => 'int'),
|
||||
'updated_time' => array('public' => 'int'),
|
||||
'parent_id' => array('public' => 'string'),
|
||||
'owner_id' => array('public' => 'string'),
|
||||
'is_encrypted' => array('public' => 'bool'),
|
||||
'encryption_method' => array('public' => 'string'),
|
||||
'is_default' => array('public' => 'bool'),
|
||||
);
|
||||
|
||||
public function add($ids) {
|
||||
@ -35,6 +35,10 @@ class Folder extends BaseItem {
|
||||
return Folder::where('owner_id', '=', $ownerId)->count();
|
||||
}
|
||||
|
||||
static public function defaultFolder($ownerId) {
|
||||
return self::where('owner_id', '=', $ownerId)->where('is_default', '=', 1)->first();
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
if (self::countByOwnerId($this->owner_id) <= 1) throw new \Exception('Cannot delete the last folder');
|
||||
|
||||
@ -45,4 +49,31 @@ class Folder extends BaseItem {
|
||||
return parent::delete();
|
||||
}
|
||||
|
||||
public function save(Array $options = array()) {
|
||||
$dirty = $this->getDirty();
|
||||
|
||||
// Handling of default folder is done in several steps:
|
||||
// - If changing is_default to false and this is the only default folder - throw an exception.
|
||||
// - Then save the folder
|
||||
// - Then, if the folder was set to be the new default, set all the other folders to non-default.
|
||||
|
||||
if (isset($dirty['is_default'])) {
|
||||
$defaultFolders = self::where('owner_id', '=', $this->owner_id)->where('is_default', '=', 1);
|
||||
|
||||
if (!$dirty['is_default'] && $defaultFolders->count() == 1 && self::defaultFolder($this->owner_id)->id == $this->id) {
|
||||
throw new \Exception(sprintf('Cannot make folder %s non-default - there should be at least one default folder', BaseModel::hex($this->id)));
|
||||
}
|
||||
}
|
||||
|
||||
$output = parent::save($options);
|
||||
|
||||
if ($output && isset($dirty['is_default'])) {
|
||||
if (!!$dirty['is_default']) {
|
||||
self::where('owner_id', '=', $this->owner_id)->where('id', '!=', $this->id)->update(array('is_default' => 0));
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
c:
|
||||
C:\Users\Laurent\AppData\Local\Android\sdk\tools
|
||||
emulator.exe -avd Nexus_5X_API_23_Google_API_
|
36
tests/Controller/FoldersControllerTest.php
Normal file
36
tests/Controller/FoldersControllerTest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
require_once dirname(dirname(__FILE__)) . '/setup.php';
|
||||
|
||||
use AppBundle\Model\Folder;
|
||||
|
||||
class FoldersControllerTest extends BaseControllerTestCase {
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
Folder::truncate();
|
||||
}
|
||||
|
||||
public function testDefault() {
|
||||
// $this->loadSession(1, 1);
|
||||
|
||||
// $f1 = $this->request('POST', '/folders', null, array(
|
||||
// 'title' => 'first folder',
|
||||
// 'is_default' => true,
|
||||
// ));
|
||||
|
||||
// $this->assertArrayHasKey('is_default', $f1);
|
||||
// $this->assertTrue($f1['is_default']);
|
||||
|
||||
// $f2 = $this->request('POST', '/folders', null, array(
|
||||
// 'title' => 'first folder',
|
||||
// 'is_default' => true,
|
||||
// ));
|
||||
|
||||
// $f1 = $this->request('GET', '/folders/' . $f1['id']);
|
||||
|
||||
// $this->assertFalse($f1['is_default']);
|
||||
}
|
||||
|
||||
}
|
19
tests/Model/BaseModelTest.php
Normal file
19
tests/Model/BaseModelTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
require_once dirname(dirname(__FILE__)) . '/setup.php';
|
||||
|
||||
use AppBundle\Model\Folder;
|
||||
use AppBundle\Model\BaseModel;
|
||||
|
||||
class BaseModelTest extends BaseTestCase {
|
||||
|
||||
public function testDbValueToPublicValue() {
|
||||
$this->assertEquals(true, Folder::dbValueToPublicValue('is_default', '1'));
|
||||
$this->assertEquals(true, Folder::dbValueToPublicValue('is_default', true));
|
||||
$this->assertEquals(false, Folder::dbValueToPublicValue('is_default', 0));
|
||||
$this->assertEquals(false, Folder::dbValueToPublicValue('is_default', '0'));
|
||||
$this->assertEquals(123, Folder::dbValueToPublicValue('created_time', '123'));
|
||||
$this->assertEquals('the title', Folder::dbValueToPublicValue('title', 'the title'));
|
||||
}
|
||||
|
||||
}
|
46
tests/Model/FolderTest.php
Normal file
46
tests/Model/FolderTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
require_once dirname(dirname(__FILE__)) . '/setup.php';
|
||||
|
||||
use AppBundle\Model\Folder;
|
||||
|
||||
class FolderTest extends BaseTestCase {
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
Folder::truncate();
|
||||
}
|
||||
|
||||
public function testDefault() {
|
||||
$f1 = new Folder();
|
||||
$f1->is_default = true;
|
||||
$f1->owner_id = TestUtils::userId();
|
||||
$f1->save();
|
||||
|
||||
$f2 = new Folder();
|
||||
$f2->is_default = true;
|
||||
$f2->owner_id = TestUtils::userId();
|
||||
$f2->save();
|
||||
|
||||
$f1 = Folder::find($f1->id);
|
||||
$f2 = Folder::find($f2->id);
|
||||
|
||||
$this->assertTrue(!$f1->is_default);
|
||||
$this->assertTrue(!!$f2->is_default);
|
||||
}
|
||||
|
||||
public function testDefaultException() {
|
||||
$this->expectException(Exception::class);
|
||||
|
||||
$f1 = new Folder();
|
||||
$f1->is_default = true;
|
||||
$f1->owner_id = TestUtils::userId();
|
||||
$f1->save();
|
||||
|
||||
$f1 = Folder::find($f1->id);
|
||||
$f1->is_default = false;
|
||||
$f1->save();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user