Esempio n. 1
0
 public function testCantCreateIfMandateIsExpired()
 {
     Yii::app()->db->createCommand()->update('mandate', array('expiration_ts' => '2014-04-21 00:00:00'), 'id = 1');
     $mandate = Mandate::model()->findByPk(1);
     $this->assertFalse($mandate->isActive());
     $petition = new Petition();
     $petition->title = 'Some petition';
     $petition->content = 'Petition content';
     $petition->mandate_id = 1;
     $petition->creator_id = 2;
     $this->assertFalse($petition->save());
 }
Esempio n. 2
0
 public function handleCreatePetition()
 {
     $petition = new Petition();
     $petition->class_name = Input::get('class_name');
     $petition->class_desc = Input::get('class_desc');
     $petition->subject = Input::get('subject');
     if (!$petition->validate(Input::all())) {
         return Redirect::back()->withInput()->withErrors(array_merge($petition->getErrors()->toArray()));
     } else {
         $petition->save();
         return Redirect::to('petitions')->with(array('alert' => 'Successful! Don\'t forget to sign your own petition', 'alert-class' => 'alert-success'));
     }
 }
Esempio n. 3
0
 public function testRateSavesAfterValidation()
 {
     $petition = new Petition();
     $petition->title = 'Some petition';
     $petition->content = 'Petition content';
     $petition->mandate_id = 1;
     $petition->creator_id = 2;
     $petition->save();
     $petitionRate = new PetitionRate();
     $petitionRate->target_id = $petition->id;
     $petitionRate->user_id = 2;
     $petitionRate->score = PetitionRate::SCORE_POSITIVE;
     $this->assertTrue($petitionRate->validate());
     $this->assertTrue($petitionRate->save());
 }
Esempio n. 4
0
 /**
  * Creates new petition by ajax request
  */
 public function actionAjaxCreate()
 {
     $mandateId = isset($_POST['mandateId']) ? (int) $_POST['mandateId'] : false;
     if (!$mandateId && isset($_POST['Petition'])) {
         $mandateId = $_POST['Petition']['mandate_id'];
     }
     if (!$mandateId) {
         throw new CHttpException(403, 'New petition can be created for existing mandate only');
     }
     $model = new Petition();
     $userId = Yii::app()->user->id;
     $mandate = Mandate::model()->findByPk($mandateId);
     if (!$mandate) {
         throw new CHttpException(404, "Specified mandate was not found");
     }
     if (!$userId || !$mandate->acceptsPetitionFrom($userId)) {
         throw new CHttpException(403, 'Petition can be created by mandate\'s adherents only');
     }
     if (isset($_POST['Petition'])) {
         $jsonResponse = array('success' => false, 'responseHtml' => '');
         $model->attributes = $_POST['Petition'];
         if ($model->save()) {
             $jsonResponse['success'] = true;
         }
     } else {
         $model->creator_id = $userId;
         $model->mandate_id = $mandate->id;
     }
     $formHtml = $this->renderPartial('_form', array('model' => $model, 'forAjax' => true), true);
     if (!isset($jsonResponse)) {
         echo $formHtml;
     } else {
         $jsonResponse['responseHtml'] = $formHtml;
         echo CJSON::encode($jsonResponse);
     }
     Yii::app()->end();
 }