Skip to content

Commit

Permalink
Fixed #19: Wrong sort calculation when prepending new model to one model
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Sep 28, 2015
1 parent 3845c66 commit 2d98b18
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/behaviors/numerical/BaseNumericalSortableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ protected function resolveConflict($newPosition, $updateCurrentModel = true)
foreach ($models as $model) {
$isCurrentModel = $model->primaryKey == $this->model->primaryKey;

if ($position == $newPosition && $position != $this->getSortableCount()) {
if ($position == $newPosition) {
$position++;
}

Expand Down
12 changes: 11 additions & 1 deletion tests/_data/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Category extends ActiveRecord
public function behaviors()
{
return [
[
'sort' => [
'class' => ContinuousNumericalSortableBehavior::className(),
'scope' => function ($model) {
/* @var $model Category */
Expand All @@ -37,6 +37,16 @@ public static function tableName()
return 'categories';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
['name', 'string', 'max' => 255],
];
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down
1 change: 1 addition & 0 deletions tests/_data/dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (5, NULL, 'C
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (6, 2, 'Category 2.1', 1);
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (7, 2, 'Category 2.2', 2);
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (8, 2, 'Category 2.3', 3);
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (9, 8, 'Category 2.3.1', 1);

COMMIT;
17 changes: 16 additions & 1 deletion tests/unit/ContinuousTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public function testCreatePrependAdded()
$question->save();

$this->checkQuestion($question, 1, [2, 3, 4, 5, 6]);

// Prepend to scope with single model
$category = new Category();
$behaviorConfig = $category->behaviors()['sort'];
$behaviorConfig['prependAdded'] = true;
$category->detachBehavior('sort');
$category->attachBehavior('sort', $behaviorConfig);
$category->setAttributes([
'parent_id' => 8,
'name' => 'Category 2.3.2',
], false);
$category->save();
$sort = Category::find()->select('sort')->orderBy(['id' => SORT_ASC])->column();

$this->assertEquals([1, 1, 2, 3, 2, 1, 2, 3, 2, 1], $sort);
}

public function testUpdate()
Expand Down Expand Up @@ -101,7 +116,7 @@ public function testMoveToOtherScope()
$category->moveAfter(7);
$sort = Category::find()->select('sort')->orderBy(['id' => SORT_ASC])->column();

$this->assertEquals([1, 1, 3, 2, 2, 1, 2, 4], $sort);
$this->assertEquals([1, 1, 3, 2, 2, 1, 2, 4, 1], $sort);
}

public function testMoveToPosition()
Expand Down

0 comments on commit 2d98b18

Please sign in to comment.