Skip to content

Commit

Permalink
add methods (luyadev#369)
Browse files Browse the repository at this point in the history
* add methods

* add basic test

* add api test

* add test in seperate process

* test not found tag and empty tags action

* add changelog
  • Loading branch information
nadar authored and slowfox089 committed Dec 10, 2020
1 parent 32d8463 commit a1f6c22
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

## 2.2.1

+ [#369](https://github.com/luyadev/luya-module-admin/pull/369) Added `toggleRelation` option for Tags model and common api to toggle tags on a certain item.
+ [#367](https://github.com/luyadev/luya-module-admin/issues/367) Fixed bug with checkbox properties and default values in admin context.

## 2.2.0 (17. September 2019)
Expand Down
60 changes: 30 additions & 30 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/apis/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use yii\data\ActiveDataProvider;
use yii\web\ForbiddenHttpException;
use luya\admin\models\Config;
use yii\base\InvalidCallException;
use yii\web\NotFoundHttpException;

/**
Expand Down Expand Up @@ -133,6 +134,26 @@ public function actionTags()
return Tag::find()->select(['id', 'name', 'translation'])->orderBy(['name' => SORT_ASC])->all();
}

/**
* Toggle the state of a given tag for a certain relation.
*
* @param integer $tagId
* @param integer $pkId
* @param string $tableName
* @return boolean
* @since 2.2.1
*/
public function actionTagRelationToggle($tagId, $pkId, $tableName)
{
$tag = Tag::findOne($tagId);

if (!$tag) {
throw new InvalidCallException("The given tag id does not exists.");
}

return $tag->toggleRelation($pkId, $tableName);
}

/**
* Set the lastest ngrest filter selection in the User Settings.
*
Expand Down
53 changes: 53 additions & 0 deletions src/models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use luya\admin\ngrest\base\NgRestModel;
use luya\admin\Module;
use luya\admin\traits\TaggableTrait;
use yii\db\ActiveRecordInterface;

/**
* This is the model class for table "admin_tag".
*
* @property integer $id
* @property string $name
* @property string $translation
*
* @author Basil Suter <[email protected]>
* @since 1.0.0
Expand Down Expand Up @@ -142,6 +144,57 @@ public static function findRelationsTable($tableName)
->all();
}

/**
* Toggle (Enable or Disable) a given tag for a Model.
*
* ```php
* $tag = Tag::find()->where(['alias' => 'soccer'])->one();
*
* $model = MyModel::findOne(1);
* $tag->toggleRelationByModel($model);
* ```
*
* @param ActiveRecordInterface $model
* @return boolean
* @since 2.2.1
*/
public function toggleRelationByModel(ActiveRecordInterface $model)
{
$pkId = $model->getPrimaryKey(false);
$tableName = TaggableTrait::cleanBaseTableName($model->tableName());

return $this->toggleRelation($pkId, $tableName);
}

/**
* Toggle a tag relation for given pkId and tableName.
*
* @param integer $pkId
* @param string $tableName
* @return boolean
* @since 2.2.1
*/
public function toggleRelation($pkId, $tableName)
{
$relation = $this->getTagRelations()
->andWhere([
'table_name' => $tableName,
'pk_id' => $pkId,
])
->one();

if ($relation) {
return (bool) $relation->delete();
}

$relation = new TagRelation();
$relation->tag_id = $this->id;
$relation->table_name = $tableName;
$relation->pk_id = $pkId;

return $relation->save();
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down
85 changes: 85 additions & 0 deletions tests/admin/apis/CommonControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace luya\admin\tests\admin\apis;

use admintests\AdminModelTestCase;
use luya\admin\apis\CommonController;
use luya\admin\models\Tag;
use luya\admin\models\TagRelation;
use luya\testsuite\fixtures\ActiveRecordFixture;
use luya\testsuite\fixtures\NgRestModelFixture;
use luya\testsuite\scopes\PermissionScope;
use luya\testsuite\traits\AdminDatabaseTableTrait;

class CommonControllerTest extends AdminModelTestCase
{
use AdminDatabaseTableTrait;

/**
* @runInSeparateProcess
*/
public function testActionTagRelationToggle()
{
PermissionScope::run($this->app, function(PermissionScope $scope) {

$lang = $this->createAdminLangFixture([]);

$tag = new NgRestModelFixture([
'modelClass' => Tag::class,
'fixtureData' => [
'tag1' => [
'id' => 1,
'name' => '#barfoo'
]
]
]);

$rel = new ActiveRecordFixture([
'modelClass' => TagRelation::class,
]);

$scope->createAndAllowRoute('adminmodeltest/id/tag-relation-toggle');

$ctrl = new CommonController('id', $this->app);

$response = $scope->runControllerAction($ctrl, 'tag-relation-toggle', [
'tagId' => 1,
'pkId' => 2,
'tableName' => 'test',
]);

$this->assertTrue($response);

$this->expectException('yii\base\InvalidCallException');
$response = $scope->runControllerAction($ctrl, 'tag-relation-toggle', [
'tagId' => 100,
'pkId' => 2,
'tableName' => 'test',
]);

$tag->cleanup();
$rel->cleanup();
$lang->cleanup();
});
}

/**
* @runInSeparateProcess
*/
public function testActionTags()
{
PermissionScope::run($this->app, function(PermissionScope $scope) {
$this->createAdminLangFixture([]);
$scope->createAndAllowRoute('adminmodeltest/id/tags');
$ctrl = new CommonController('id', $this->app);
$tag = new NgRestModelFixture([
'modelClass' => Tag::class,
'fixtureData' => [
]
]);
$response = $scope->runControllerAction($ctrl, 'tags');

$this->assertSame([], $response);
});
}
}
Loading

0 comments on commit a1f6c22

Please sign in to comment.