Exemple #1
0
 public function testMandatesForUser()
 {
     $mandates = Mandate::model()->getUsersMandates(1);
     $this->assertCount(1, $mandates);
     $this->assertEquals(1, $mandates[0]->id);
     $this->assertEquals('Mandate of Election 1', $mandates[0]->name);
 }
 public function actionPetitions()
 {
     $usersMandates = array();
     $mandates = Mandate::model()->getUsersMandates($this->profile->user_id);
     foreach ($mandates as $key => $mandate) {
         $usersMandates[] = $mandate->id;
     }
     $usersMandates = '[' . implode(',', $usersMandates) . ']';
     $this->render('petitions', array('usersMandates' => $usersMandates));
 }
Exemple #3
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());
 }
Exemple #4
0
 public function testCreateMandateMethod()
 {
     $candidate = $this->getFixtureManager()->getRecord('candidate', 3);
     $electedCandidateId = $candidate->id;
     $election = Election::model()->findByPk($candidate->election_id);
     $election->quote = 1;
     $election->status = Election::STATUS_REGISTRATION;
     $this->assertTrue($election->save());
     $election->status = Election::STATUS_ELECTION;
     $this->assertTrue($election->save());
     $vote = new Vote();
     $vote->candidate_id = $electedCandidateId;
     $vote->election_id = $candidate->election_id;
     $vote->user_id = $election->electors[0]->user_id;
     $this->assertTrue($vote->save(), print_r($vote->getErrors(), true));
     $election->status = Election::STATUS_FINISHED;
     $election->save();
     $mandate = Mandate::model()->findByAttributes(array('election_id' => $candidate->election_id, 'candidate_id' => $candidate->id));
     $this->assertEquals($election->mandate, $mandate->name);
     $this->assertEquals($election->validity, $mandate->validity);
 }
 public function testPetitionsLists()
 {
     $this->open('userPage/petitions/1');
     $this->waitForPresent($this->getCssSel('container'));
     $this->waitForPresent($this->getCssSel('myPetitions.petitionsFeed'));
     $this->waitForPresent($this->getCssSel('petitionsForMe.petitionsFeed'));
     $this->assertPetitionsTotalCountIs($count = 15, 'petitionsForMe');
     $this->assertPetitionsOnPageCountIs($count, 'petitionsForMe');
     $this->assertPetitionsTotalCountIs($count = 16, 'myPetitions');
     $this->assertPetitionsOnPageCountIs($count, 'myPetitions');
     $petitions = Petition::model()->findAllByAttributes(array('creator_id' => 1));
     $this->assertPetitionsFeedContains($petitions, 'myPetitions');
     $petitions = Petition::model()->findAllByAttributes(array('mandate_id' => Mandate::model()->getUsersMandates(1)));
     $this->assertPetitionsFeedContains($petitions, 'petitionsForMe');
     $this->open('userPage/petitions/2');
     $this->waitForPresent($this->getCssSel('container'));
     $this->waitForPresent($this->getCssSel('petitionsFeed'));
     $this->assertElementNotPresent($this->getCssSel('myPetitions'));
     $this->assertElementNotPresent($this->getCssSel('petitionsForMe'));
     $this->assertPetitionsTotalCountIs(0);
     $this->assertElementContainsText($this->getCssSel('petitionsFeed.item'), 'There is no items.');
 }
 /**
  * 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();
 }
Exemple #7
0
 /**
  * @param Candidate $candidate
  * @return Mandate Mandate created for candidate
  */
 public function createMandate($candidate)
 {
     $election = $this->getMachine()->getOwner();
     $mandate = new Mandate();
     $mandate->name = $election->mandate;
     $mandate->validity = $election->validity;
     $mandate->election_id = $election->id;
     $mandate->candidate_id = $candidate->id;
     $mandate->votes_count = $candidate->acceptedVotesCount;
     $mandate->submiting_ts = date('Y-m-d');
     $exp = new DateTime();
     $exp->add(new DateInterval('P' . $election->validity . 'M'));
     $mandate->expiration_ts = $exp->format('Y-m-d');
     if (!$mandate->save()) {
         throw new Exception('Can\'t create mandate. Validation errors: ' . print_r($mandate->getErrors(), true));
     }
     return $mandate;
 }