public function testAgoInFuture() { DBDatetime::set_mock_now('2000-12-31 00:00:00'); $this->assertEquals('in 10 years', DBField::create_field('Date', '2010-12-31')->Ago(), 'Exact past match on years'); $this->assertEquals('in 1 day', DBField::create_field('Date', '2001-01-01')->Ago(true, 1), 'Approximate past match on minutes'); $this->assertEquals('in 24 hours', DBField::create_field('Date', '2001-01-01')->Ago(), 'Approximate past match on minutes'); DBDatetime::clear_mock_now(); }
public function tearDown() { // Preserve memory settings ini_set('memory_limit', $this->originalMemoryLimit ? $this->originalMemoryLimit : -1); // Restore email configuration $this->originalMailer = null; $this->mailer = null; // Restore password validation if ($this->originalMemberPasswordValidator) { Member::set_password_validator($this->originalMemberPasswordValidator); } // Restore requirements if ($this->originalRequirements) { Requirements::set_backend($this->originalRequirements); } // Mark test as no longer being run - we use originalIsRunningTest to allow for nested SapphireTest calls self::$is_running_test = $this->originalIsRunningTest; $this->originalIsRunningTest = null; // Reset mocked datetime DBDatetime::clear_mock_now(); // Stop the redirection that might have been requested in the test. // Note: Ideally a clean Controller should be created for each test. // Now all tests executed in a batch share the same controller. $controller = Controller::has_curr() ? Controller::curr() : null; if ($controller && $controller->response && $controller->response->getHeader('Location')) { $controller->response->setStatusCode(200); $controller->response->removeHeader('Location'); } Versioned::set_reading_mode($this->originalReadingMode); //unnest injector / config now that tests are over Injector::unnest(); Config::unnest(); }
public function testExpiredRememberMeHashAutologin() { $m1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'noexpiry'); $m1->login(true); $firstHash = RememberLoginHash::get()->filter('MemberID', $m1->ID)->First(); $this->assertNotNull($firstHash); // re-generates the hash so we can get the token $firstHash->Hash = $firstHash->getNewHash($m1); $token = $firstHash->getToken(); $firstHash->ExpiryDate = '2000-01-01 00:00:00'; $firstHash->write(); DBDateTime::set_mock_now('1999-12-31 23:59:59'); $response = $this->get('Security/login', $this->session(), null, array('alc_enc' => $m1->ID . ':' . $token, 'alc_device' => $firstHash->DeviceID)); $message = _t('Member.LOGGEDINAS', "You're logged in as {name}.", array('name' => $m1->FirstName)); $this->assertContains($message, $response->getBody()); $this->session()->inst_set('loggedInAs', null); // re-generates the hash so we can get the token $firstHash->Hash = $firstHash->getNewHash($m1); $token = $firstHash->getToken(); $firstHash->ExpiryDate = '2000-01-01 00:00:00'; $firstHash->write(); DBDateTime::set_mock_now('2000-01-01 00:00:01'); $response = $this->get('Security/login', $this->session(), null, array('alc_enc' => $m1->ID . ':' . $token, 'alc_device' => $firstHash->DeviceID)); $this->assertNotContains($message, $response->getBody()); $this->session()->inst_set('loggedInAs', null); DBDatetime::clear_mock_now(); }
public function testAllVersions() { // In 2005 this file was created DBDatetime::set_mock_now('2005-01-01 00:00:00'); $testPage = new VersionedTest_Subclass(); $testPage->Title = 'Archived page'; $testPage->Content = 'This is the content from 2005'; $testPage->ExtraField = '2005'; $testPage->write(); // In 2007 we updated it DBDatetime::set_mock_now('2007-01-01 00:00:00'); $testPage->Content = "It's 2007 already!"; $testPage->ExtraField = '2007'; $testPage->write(); // Check both versions are returned $versions = Versioned::get_all_versions('VersionedTest_Subclass', $testPage->ID); $content = array(); $extraFields = array(); foreach ($versions as $version) { $content[] = $version->Content; $extraFields[] = $version->ExtraField; } $this->assertEquals($versions->Count(), 2, 'All versions returned'); $this->assertEquals($content, array('This is the content from 2005', "It's 2007 already!"), 'Version fields returned'); $this->assertEquals($extraFields, array('2005', '2007'), 'Version fields returned'); // In 2009 we updated it again DBDatetime::set_mock_now('2009-01-01 00:00:00'); $testPage->Content = "I'm enjoying 2009"; $testPage->ExtraField = '2009'; $testPage->write(); // End mock, back to the present day:) DBDatetime::clear_mock_now(); $versions = Versioned::get_all_versions('VersionedTest_Subclass', $testPage->ID); $content = array(); $extraFields = array(); foreach ($versions as $version) { $content[] = $version->Content; $extraFields[] = $version->ExtraField; } $this->assertEquals($versions->Count(), 3, 'Additional all versions returned'); $this->assertEquals($content, array('This is the content from 2005', "It's 2007 already!", "I'm enjoying 2009"), 'Additional version fields returned'); $this->assertEquals($extraFields, array('2005', '2007', '2009'), 'Additional version fields returned'); }
public function testAgoInFuture() { DBDatetime::set_mock_now('2000-12-31 00:00:00'); $this->assertEquals('in 10 years', DBField::create_field('Datetime', '2010-12-31 12:00:00')->Ago(), 'Exact past match on years'); $this->assertEquals('in 1 hour', DBField::create_field('Datetime', '2000-12-31 1:01:05')->Ago(true, 1), 'Approximate past match on minutes, significance=1'); $this->assertEquals('in 61 mins', DBField::create_field('Datetime', '2000-12-31 1:01:05')->Ago(), 'Approximate past match on minutes'); DBDatetime::clear_mock_now(); }