forked from luyadev/luya-module-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
32d8463
commit a1f6c22
Showing
6 changed files
with
246 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
Oops, something went wrong.