/**
  * @param RedBeanModel $model
  * @param RedBeanModel $copyToModel
  */
 public static function copy(RedBeanModel $model, RedBeanModel $copyToModel)
 {
     parent::copy($model, $copyToModel);
     foreach ($model->checkListItems as $checkListItem) {
         $taskCheckListItem = new TaskCheckListItem();
         $taskCheckListItem->name = $checkListItem->name;
         $taskCheckListItem->sortOrder = $checkListItem->sortOrder;
         $copyToModel->checkListItems->add($taskCheckListItem);
     }
 }
 public function testCopy()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $accounts = Account::getByName('anAccount');
     $task = new Task();
     $task->name = 'My Task';
     $task->owner = Yii::app()->user->userModel;
     $task->completedDateTime = '0000-00-00 00:00:00';
     $task->activityItems->add($accounts[0]);
     $saved = $task->save();
     $this->assertTrue($saved);
     $taskId = $task->id;
     $task->forget();
     unset($task);
     $task = Task::getById($taskId);
     $copyToTask = new Task();
     ActivityCopyModelUtil::copy($task, $copyToTask);
     $this->assertEquals('My Task', $copyToTask->name);
     $this->assertEquals(1, $copyToTask->activityItems->count());
 }
 public function testSuperUserCopyAction()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $superAccountId = self::getModelIdByModelNameAndName('Account', 'superAccount');
     $superContactId = self::getModelIdByModelNameAndName('Contact', 'superContact superContactson');
     $account = Account::getById($superAccountId);
     $contact = Contact::getById($superContactId);
     $activityItemPostData = array('Account' => array('id' => $superAccountId), 'Contact' => array('id' => $superContactId));
     $this->setGetArray(array('relationAttributeName' => 'Account', 'relationModelId' => $superAccountId, 'relationModuleId' => 'accounts', 'redirectUrl' => 'someRedirect'));
     $this->setPostArray(array('ActivityItemForm' => $activityItemPostData, 'Task' => array('name' => 'myTask', 'description' => 'Some task description')));
     $this->runControllerWithRedirectExceptionAndGetContent('tasks/default/createFromRelation');
     $tasks = Task::getByName('myTask');
     $this->assertCount(1, $tasks);
     $this->setGetArray(array('id' => $tasks[0]->id));
     $this->resetPostArray();
     $content = $this->runControllerWithNoExceptionsAndGetContent('tasks/default/copy');
     $this->assertContains($tasks[0]->name, $content);
     $this->assertContains($tasks[0]->description, $content);
     $this->assertContains($account->name, $content);
     $this->assertContains($contact->getFullName(), $content);
     $taskCopy = new Task();
     ActivityCopyModelUtil::copy($tasks[0], $taskCopy);
     $activityItemPostData = array();
     foreach ($taskCopy->activityItems as $relatedModel) {
         $activityItemPostData[get_class($relatedModel)] = array('id' => $relatedModel->id);
     }
     $postArray = array('Task' => array('name' => $taskCopy->name, 'description' => $taskCopy->description), 'ActivityItemForm' => $activityItemPostData);
     $this->setPostArray($postArray);
     $this->setGetArray(array('id' => $tasks[0]->id));
     $content = $this->runControllerWithRedirectExceptionAndGetContent('tasks/default/copy');
     $tasks = Task::getByName('myTask');
     $this->assertCount(2, $tasks);
     $this->assertEquals($tasks[0]->name, $tasks[1]->name);
     $this->assertEquals($tasks[0]->description, $tasks[1]->description);
     $this->assertEquals($tasks[0]->activityItems[0], $tasks[1]->activityItems[0]);
     $this->assertEquals($tasks[0]->activityItems[1], $tasks[1]->activityItems[1]);
     $tasks[0]->delete();
     $tasks[1]->delete();
 }
 /**
  * @param $id
  * @param null $redirectUrl
  */
 public function actionCopy($id, $redirectUrl = null)
 {
     $modelClassName = $this->getModule()->getPrimaryModelName();
     $copyToActivity = new $modelClassName();
     $postVariableName = get_class($copyToActivity);
     if (!isset($_POST[$postVariableName])) {
         $activity = $modelClassName::getById((int) $id);
         ControllerSecurityUtil::resolveAccessCanCurrentUserReadModel($activity);
         ActivityCopyModelUtil::copy($activity, $copyToActivity);
     }
     $this->processEdit($copyToActivity, $redirectUrl);
 }