Enh: Enhanced ContentActiveRecord instantiation $model = new MyContent($space, Content::VISIBILITY_PRIVATE)

This commit is contained in:
buddh4 2017-06-14 13:10:54 +02:00
parent cc43601ecd
commit 3d40641fce
3 changed files with 73 additions and 0 deletions

View File

@ -32,6 +32,7 @@ HumHub Change Log
- Enh: Allow search in country profile field dropdown
- Fix: js action api empty data attribute
- Enh: Added button helper widgets `<?= Button::primary('myButton')->action('myJsAction')?>`
- Enh: Enhanced ContentActiveRecord instantiation `$model = new MyContent($space, Content::VISIBILITY_PRIVATE)`
1.2.0 (April 16, 2017)
--------------------------------

View File

@ -81,6 +81,36 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner
*/
protected $managePermission = ManageContent::class;
/**
* ContentActiveRecord constructor accepts either an configuration array as first argument or an ContentContainerActiveRecord
* and visibility settings.
*
* Use as follows:
*
* `$model = new MyContent(['myField' => 'value']);`
*
* or
*
* `$model = new MyContent($space1, Content::VISIBILITY_PUBLIC, ['myField' => 'value']);`
*
*
* @param array|ContentContainerActiveRecord $contentContainer either the configuration or contentcontainer
* @param int $visibility
* @param array $config
*/
public function __construct($contentContainer = [], $visibility = Content::VISIBILITY_PRIVATE, $config = [])
{
if(is_array($contentContainer)) {
parent::__construct($contentContainer);
} else if($contentContainer instanceof ContentContainerActiveRecord) {
$this->content->setContainer($contentContainer);
$this->content->visibility = $visibility;
parent::__construct($config);
} else {
parent::__construct([]);
}
}
/**
* @inheritdoc
*/

View File

@ -0,0 +1,42 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
namespace tests\codeception\unit\modules\content;
use Yii;
use tests\codeception\_support\HumHubDbTestCase;
use Codeception\Specify;
use humhub\modules\post\models\Post;
use humhub\modules\space\models\Space;
use humhub\modules\content\models\Content;
use humhub\modules\stream\actions\ContentContainerStream;
class ContentApiTest extends HumHubDbTestCase
{
use Specify;
public function testConstructor()
{
$this->becomeUser('User2');
$space = Space::findOne(['id' => 2]);
$post1 = new Post($space, Content::VISIBILITY_PUBLIC);
$this->assertEquals($space->id, $post1->content->container->id);
$this->assertEquals(Content::VISIBILITY_PUBLIC, $post1->content->visibility);
$post2 = new Post($space, Content::VISIBILITY_PRIVATE, ['message' => 'Hello!']);
$this->assertEquals($space->id, $post2->content->container->id);
$this->assertEquals(Content::VISIBILITY_PRIVATE, $post2->content->visibility);
$this->assertEquals('Hello!', $post2->message);
$post3 = new Post(['message' => 'Hello!']);
$this->assertEquals('Hello!', $post3->message);
}
}