/** * Test * * @test * @return void */ public function storeNew() { $now = new DateTime(); $user = new User(); $user->setCreated($now); $user->save(); $user = User::getById(4); $this->assertEquals($now, $user->getCreated()); }
/** * test #14. * Overriding automatic attributes using a future date. Because * a future date is used, the record can no longer be changed after * it was saved. * @depends testSaveEmptyObject * @depends testSetAttributes * @depends testUpdate * @depends testOverrideAutomaticAttributesNewRecord */ public function testOverrideAutomaticAttributesFutureDate() { global $testUserId1; // Create the object, which automatically gets the current date $object = new User($testUserId1); $object->setName('Test User'); $object->setExternalType('externaltype'); $object->setExternalId('externalid'); $object->setAccess('access'); $object->setEmail('*****@*****.**'); $object->setNotification('Y'); $object->setTempCode('tempcode'); $object->setDeleted('Y'); $this->assertTrue($object->save()); $this->assertEquals(1, $this->countTestRows()); $originalCreated = $object->getCreated(); $originalUpdated = $object->getUpdated(); $originalHash = $object->getHash(); // Change the object with different values, using a guaranteed // future date for the Created and Updated fields. Note that // the mySQL timestamp values allow for dates up to January 19, // 2038. Select as the future date for this test January 18, 2038 // values after first save are unchanged $object->setCreated('2038-01-18 10:10:10.000000'); $object->setUpdated('2038-01-18 10:10:11.000000'); $object->setName('Test User 2'); $object->setExternalType('externaltype 2'); $object->setExternalId('externalid 2'); $object->setAccess('access 2'); $object->setEmail('*****@*****.**'); $object->setNotification('N'); $object->setTempCode('tempcode 2'); $object->setDeleted('N'); $object->setHash('future date hash'); // Check the values before saving $this->assertEquals($testUserId1, $object->getUserId()); $this->assertEquals('2038-01-18 10:10:10.000000', $object->getCreated()); $this->assertEquals('2038-01-18 10:10:11.000000', $object->getUpdated()); $this->assertEquals('Test User 2', $object->getName()); $this->assertEquals('externaltype 2', $object->getExternalType()); $this->assertEquals('externalid 2', $object->getExternalId()); $this->assertEquals('access 2', $object->getAccess()); $this->assertEquals('*****@*****.**', $object->getEmail()); $this->assertEquals('N', $object->getNotification()); $this->assertEquals('tempcode 2', $object->getTempCode()); $this->assertEquals('N', $object->getDeleted()); $this->assertEquals('future date hash', $object->getHash()); // update the record, this adds a row in the database $this->assertTrue($object->save()); $this->assertEquals(2, $this->countTestRows()); // after the update, the information has been saved $this->assertEquals($testUserId1, $object->getUserId()); $this->assertEquals('2038-01-18 10:10:10.000000', $object->getCreated()); $this->assertEquals('2038-01-18 10:10:11.000000', $object->getUpdated()); $this->assertEquals('Test User 2', $object->getName()); $this->assertEquals('externaltype 2', $object->getExternalType()); $this->assertEquals('externalid 2', $object->getExternalId()); $this->assertEquals('access 2', $object->getAccess()); $this->assertEquals('*****@*****.**', $object->getEmail()); $this->assertEquals('N', $object->getNotification()); $this->assertEquals('tempcode 2', $object->getTempCode()); $this->assertEquals('N', $object->getDeleted()); $this->assertEquals('future date hash', $object->getHash()); // Try to update the record. This will add a row in the database $object->setName('Test User 3'); $object->setExternalType('externaltype 3'); $object->setExternalId('externalid 3'); $object->setAccess('access 3'); $object->setEmail('*****@*****.**'); $object->setNotification('Y'); $object->setTempCode('tempcode 3'); $object->setDeleted('Y'); $this->assertTrue($object->save()); $this->assertEquals(3, $this->countTestRows()); // but the new information is not saved. The previously saved // information cannot be overwritten without manually setting the // updated field. $this->assertEquals($testUserId1, $object->getUserId()); $this->assertEquals('2038-01-18 10:10:10.000000', $object->getCreated()); $this->assertEquals('2038-01-18 10:10:11.000000', $object->getUpdated()); $this->assertEquals('Test User 2', $object->getName()); $this->assertEquals('externaltype 2', $object->getExternalType()); $this->assertEquals('externalid 2', $object->getExternalId()); $this->assertEquals('access 2', $object->getAccess()); $this->assertEquals('*****@*****.**', $object->getEmail()); $this->assertEquals('N', $object->getNotification()); $this->assertEquals('tempcode 2', $object->getTempCode()); $this->assertEquals('N', $object->getDeleted()); // Note: this will FAIL in the current implementation! //$this->assertEquals('future date hash', $object->getHash()); }
} } } else { $response = errorResponse(RESPONSE_BAD_REQUEST, 'Need hash'); } } else { if (isPutMethod()) { $data = getPostData(); if (isset($data['userId']) && $data['userId'] !== '') { $userId = $data['userId']; $object = new User($userId); if (isset($data['passwordHash'])) { $object->setPasswordHash($data['passwordHash']); } if (isset($data['created'])) { $object->setCreated($data['created']); } if (isset($data['updated'])) { $object->setUpdated($data['updated']); } if (isset($data['name'])) { $object->setName($data['name']); } if (isset($data['externalType'])) { $object->setExternalType($data['externalType']); } if (isset($data['externalId'])) { $object->setExternalId($data['externalId']); } if (isset($data['access'])) { $object->setAccess($data['access']);