1
0
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:
Laurent Cozic 2017-06-04 13:41:15 +01:00
parent 7c43f38be5
commit fdab917043
10 changed files with 165 additions and 22 deletions

View File

@ -0,0 +1,3 @@
c:
cd c:\Users\Laurent\AppData\Local\Android\sdk\tools
emulator.exe -avd Nexus_5X_API_23_Google_API_

View File

@ -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/

View File

@ -1,9 +0,0 @@
<?php
namespace AppBundle;
class ApiSerializer {
}

View File

@ -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();

View File

@ -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'])) {

View File

@ -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;
}
}

View File

@ -1,3 +0,0 @@
c:
C:\Users\Laurent\AppData\Local\Android\sdk\tools
emulator.exe -avd Nexus_5X_API_23_Google_API_

View 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']);
}
}

View 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'));
}
}

View 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();
}
}