public function testCreateInvalidTimestamp() { $errorStack = TestBootstrap::app('errors'); $errorStack->clear(); $hour = new VolunteerHour(); $this->assertFalse($hour->create(['organization' => self::$org->id(), 'uid' => TestBootstrap::app('user')->id(), 'timestamp' => time() + 86400 * 30, 'hours' => 5])); $errors = $errorStack->errors('VolunteerHour.create'); $expected = [['error' => 'invalid_hours_timestamp', 'message' => 'Invalid date - cannot report hours that happened in the future.', 'context' => 'VolunteerHour.create', 'params' => []]]; $this->assertEquals($expected, $errors); }
public function reportHoursStep3($req, $res) { $org = $this->getOrg($req, $res); if (!is_object($org)) { return $org; } $currentUser = $this->app['user']; // make sure the user is logged in if (!$currentUser->isLoggedIn()) { setcookie('redirect', $org->url() . '/hours/report', time() + 3600, '/'); return $res->redirect('/login'); } $place = $req->query('place'); if ($place) { $place = new VolunteerPlace($place); } if (!$place || !$place->exists()) { return $res->redirect($org->manageUrl() . '/hours/add'); } // validate tags $tags = $req->request('tags'); foreach ((array) explode(' ', $tags) as $tag) { if (!Validate::is($tag, 'alpha_dash')) { $this->app['errors']->push(['error' => 'invalid_volunteer_hour_tags']); return $this->reportHoursStep2($req, $res); } } // validate day $date = \DateTime::createFromFormat('M j, Y', $req->request('timestamp')); if (!$date) { $this->app['errors']->push(['error' => 'validation_failed', 'params' => ['field_name' => 'Day']]); return $this->reportHoursStep2($req, $res); } $uid = $currentUser->id(); $hour = new VolunteerHour(); $success = $hour->create(['organization' => $org->id(), 'uid' => $uid, 'hours' => $req->request('hours'), 'timestamp' => $date->getTimestamp(), 'place' => $place->id(), 'approved' => false, 'tags' => $tags]); if ($success) { $res->redirect($org->url() . '/hours/thanks'); } else { return $this->reportHoursStep2($req, $res); } }
public function recordHoursStep4($req, $res) { $org = $this->getOrgForAdmin($req, $res); if (!is_object($org)) { return; } // go back to the edit screen if ($req->request('edit')) { return $this->recordHoursStep2($req, $res); } $place = new VolunteerPlace($req->query('place')); // decode hours from json $input = json_decode($req->request('json'), true); // create days $days = []; foreach ($input['days'] as $d => $day) { $date = \DateTime::createFromFormat('M j, Y', $day); $days[$d] = $date->getTimestamp(); } // create hours $numHours = 0; $volunteers = []; foreach ($input['hours'] as $uid => $hours) { foreach ($hours as $d => $n) { if ($n <= 0) { continue; } $hourMeta = ['organization' => $org->id(), 'hours' => $n, 'timestamp' => $days[$d], 'place' => $place->id(), 'approved' => true, 'tags' => $input['tags'], 'uid' => $uid]; $hour = new VolunteerHour(); if ($hour->create($hourMeta)) { if (!isset($volunteers[$uid])) { $volunteers[$uid] = 0; } $volunteers[$uid] += $n; $numHours += $n; } } } $req->setParams(['numAdded' => $numHours, 'numVolunteers' => count($volunteers)]); if ($numHours > 0) { return $this->hoursBrowse($req, $res); } else { return $this->recordHoursStep2($req, $res); } }
/** * @depends testCreate */ public function testOrgsWithUnapprovedHourNotifications() { $hour = new VolunteerHour(); $this->assertTrue($hour->create(['organization' => self::$org->id(), 'uid' => -2, 'hours' => 5, 'timestamp' => time(), 'approved' => false])); self::$org->load(); $this->assertEquals(2, self::$org->unapproved_hours_notify_count); $orgs = Organization::orgsWithUnapprovedHourNotifications(); $this->assertInstanceOf('infuse\\Model\\Iterator', $orgs); $orgs2 = []; foreach ($orgs as $org) { $orgs2[] = $org; } $this->assertCount(1, $orgs2); $this->assertEquals(self::$org->id(), $orgs2[0]->id()); }