Chng: humhub\modules\content\models\ContentTag:deleteAll() and humhub\modules\content\models\ContentTag:findAll() now respect the tag type condition by default

This commit is contained in:
buddh4 2018-08-23 21:15:33 +02:00
parent d407b88e4e
commit 6aab1c4758
2 changed files with 75 additions and 6 deletions

View File

@ -513,6 +513,7 @@ class ContentTag extends ActiveRecord
/**
* Deletes all tags by module id
* @param ContentContainerActiveRecord|int $contentContainer
* @return int the number of rows deleted
*/
public static function deleteByModule($contentContainer = null)
{
@ -520,15 +521,16 @@ class ContentTag extends ActiveRecord
if($contentContainer) {
$container_id = $contentContainer instanceof ContentContainerActiveRecord ? $contentContainer->contentcontainer_id : $contentContainer;
static::deleteAll(['module_id' => $instance->module_id, 'contentcontainer_id' => $container_id]);
return static::deleteAll(['module_id' => $instance->module_id, 'contentcontainer_id' => $container_id]);
} else {
static::deleteAll(['module_id' => $instance->module_id]);
return static::deleteAll(['module_id' => $instance->module_id]);
}
}
/**
* Deletes all tags by type
* @param ContentContainerActiveRecord|int $contentContainer
* @return int the number of rows deleted
*/
public static function deleteByType($contentContainer = null)
{
@ -536,12 +538,59 @@ class ContentTag extends ActiveRecord
if($contentContainer) {
$container_id = $contentContainer instanceof ContentContainerActiveRecord ? $contentContainer->contentcontainer_id : $contentContainer;
static::deleteAll(['type' => $instance->type, 'contentcontainer_id' => $container_id]);
return static::deleteAll(['type' => $instance->type, 'contentcontainer_id' => $container_id]);
} else {
static::deleteAll(['type' => $instance->type]);
return static::deleteAll(['type' => $instance->type]);
}
}
/**
* In case this function is not called on the base ContentTag function, it will ensure to that at least either a
* 'module_id' or 'type' condition is given to prevent deleting all content tags.
*
* If no 'module_id' or 'type' condition is given this function will automatically add a 'type' condition.
*
* @since 1.3.2
*/
public static function deleteAll($condition = null, $params = [])
{
if(static::class === ContentTag::class) {
return parent::deleteAll($condition, $params);
}
$instance = new static();
if(empty($condition)) {
$condition = ['type' => $instance->type];
} else if(!empty($condition) && !isset($condition['module_id']) && !isset($condition['type'])) {
$condition['type'] = $instance->type;
}
return parent::deleteAll($condition, $params);
}
/**
* Searches for all content tags of this type.
*
* @param $condition
* @since 1.3.2
* @return ActiveRecord[]
*/
public static function findAll($condition)
{
if (static::class === ContentTag::class) {
return parent::findAll($condition);
}
$instance = new static();
if(empty($condition)) {
$condition = ['type' => $instance->type];
} else {
$condition['type'] = $instance->type;
}
return parent::findAll($condition);
}
/**
* Finds instances by ContentContainerActiveRecord and optional type.
*

View File

@ -148,6 +148,28 @@ class ContentTagTest extends HumHubDbTestCase
$this->assertEquals(2, TestTag::find()->count());
}
public function testDeleteAll()
{
$space2 = Space::findOne(2);
$this->assertTrue($this->createTestTag('testTag1'));
$this->assertTrue($this->createTestTag('testTag2'));
$this->assertTrue($this->createTestTag('testTag3', $space2));
$this->assertTrue($this->createTestTag('testTag4', $space2));
$this->assertTrue($this->createOtherTestTag('testTagA'));
$this->assertTrue($this->createOtherTestTag('testTagB'));
$this->assertTrue($this->createOtherTestTag('testTagC', $space2));
$this->assertEquals(4, count(TestTag::findAll(null)));
$this->assertEquals(3, count(TestTagSameModule::findAll(null)));
$count = TestTagSameModule::deleteAll();
$this->assertEquals(3, $count);
$this->assertEquals(4, count(TestTag::findAll(null)));
$this->assertEquals(0, count(TestTagSameModule::findAll(null)));
}
public function testContentDeletion()
{
$content = Content::findOne(1);
@ -254,6 +276,4 @@ class ContentTagTest extends HumHubDbTestCase
$tag = new TestTagOtherModule($container, $name);
return $tag->save();
}
}