public function testSaveModelFromPostSuccessfulSave() { //Unfreeze since the test model is not part of the standard schema. $freezeWhenComplete = false; if (RedBeanDatabase::isFrozen()) { RedBeanDatabase::unfreeze(); $freezeWhenComplete = true; } Yii::app()->user->userModel = User::getByUsername('super'); $savedSuccessfully = false; $modelToStringValue = null; $postData = array('member' => 'abc'); $model = new OwnedSecurableTestItem(); $this->assertFalse($model->hasErrors()); $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromPost($postData, $model, $savedSuccessfully, $modelToStringValue); $this->assertTrue($savedSuccessfully); $this->assertEquals('abc', $modelToStringValue); $this->assertFalse($model->hasErrors()); $this->assertTrue($model->id > 0); //Re-freeze if needed. if ($freezeWhenComplete) { RedBeanDatabase::freeze(); } }
public function testSaveModelFromPostSuccessfulSave() { Yii::app()->user->userModel = User::getByUsername('super'); $savedSuccessfully = false; $modelToStringValue = null; $postData = array('member' => 'abc'); $model = new OwnedSecurableTestItem(); $this->assertFalse($model->hasErrors()); $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromPost($postData, $model, $savedSuccessfully, $modelToStringValue); $this->assertTrue($savedSuccessfully); $this->assertEquals('abc', $modelToStringValue); $this->assertFalse($model->hasErrors()); $this->assertTrue($model->id > 0); }
/** * @depends testCreateAndGetProductTemplateById */ public function testUpdateProductTemplateFromForm() { $user = User::getByUsername('Steven'); $currencies = Currency::getAll(); $currency = $currencies[0]; $currency->save(); $postData = array('name' => 'Editable Sell Price', 'description' => 'Template Description', 'status' => ProductTemplate::STATUS_ACTIVE, 'type' => ProductTemplate::TYPE_PRODUCT, 'sellPriceFormula' => array('type' => 1, 'discountOrMarkupPercentage' => 0), 'priceFrequency' => ProductTemplate::PRICE_FREQUENCY_ANNUALLY, 'cost' => array('currency' => array('id' => $currency->id), 'value' => 240), 'listPrice' => array('currency' => array('id' => $currency->id), 'value' => 200), 'sellPrice' => array('currency' => array('id' => $currency->id), 'value' => 170)); $controllerUtil = new ZurmoControllerUtil(); $productTemplate = new ProductTemplate(); $savedSucessfully = false; $modelToStringValue = null; $productTemplate = $controllerUtil->saveModelFromPost($postData, $productTemplate, $savedSucessfully, $modelToStringValue); $this->assertTrue($savedSucessfully); $id = $productTemplate->id; unset($productTemplate); $productTemplate = ProductTemplate::getById($id); $this->assertEquals('Editable Sell Price', $productTemplate->name); $this->assertEquals(170, $productTemplate->sellPrice->value); $this->assertEquals(240, $productTemplate->cost->value); $this->assertEquals(200, $productTemplate->listPrice->value); }
/** * Override to handle UserStatus processing. * @see ZurmoBaseController::attemptToSaveModelFromPost() */ protected function attemptToSaveModelFromPost($model, $redirectUrlParams = null, $redirect = true, $returnOnValidate = false) { assert('$model instanceof User || $model instanceof UserPasswordForm || $model instanceof UserAvatarForm'); assert('$redirectUrlParams == null || is_array($redirectUrlParams) || is_string($redirectUrlParams)'); $postVariableName = get_class($model); if (isset($_POST[$postVariableName])) { $postData = $_POST[$postVariableName]; if (isset($_POST[$postVariableName]['userStatus'])) { $userStatus = UserStatusUtil::makeByPostData($_POST[$postVariableName]); $sanitizedPostdata = UserStatusUtil::removeIfExistsFromPostData($postData); } else { $userStatus = null; $sanitizedPostdata = $postData; } $savedSucessfully = false; $modelToStringValue = null; $oldUsername = $model->username; $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromPost($sanitizedPostdata, $model, $savedSucessfully, $modelToStringValue); if ($savedSucessfully) { if ($userStatus != null) { if ($model instanceof UserPasswordForm || $model instanceof UserAvatarForm) { UserStatusUtil::resolveUserStatus($model->getModel(), $userStatus); } else { UserStatusUtil::resolveUserStatus($model, $userStatus); } } if ($model->id == Yii::app()->user->userModel->id && $model->username != $oldUsername) { //If the logged in user changes their username, a logout must occur to properly to properly //restart the session. Yii::app()->getSession()->destroy(); $this->redirect(Yii::app()->homeUrl); } $this->actionAfterSuccessfulModelSave($model, $modelToStringValue, $redirectUrlParams); } } return $model; }