import v1.0.0-RC4 | 2009-05-20

This commit is contained in:
2019-07-17 22:08:50 +02:00
commit b484e522e8
2459 changed files with 1038434 additions and 0 deletions

View File

@ -0,0 +1,146 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
require_once dirname(__FILE__) . '/../../../CaptchaImageTestSessionContainer.php';
class FeedbackControllerTests extends PHPUnit_Framework_TestCase
{
private $_response;
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
}
public function testIndexAction()
{
Setup::$front->setRequest(new TestRequest('/feedback'));
Setup::dispatch();
$this->assertContains('<form id="feedbackForm" method="post" action', $this->_response->getBody());
}
/**
* @dataProvider provideBadFormInput
*/
public function testSendWithEmptyFieldsAction($name, $email, $feedback)
{
$_POST = array(
'name' => $name,
'email' => $email,
'feedback' => $feedback,
);
Setup::$front->setRequest(new TestRequest('/feedback/send'));
Setup::dispatch();
$this->assertContains('Value is empty, but a non-empty value is required', $this->_response->getBody());
}
public function testSendWithBadEmailAction()
{
$_POST = array(
'name' => 'john doe',
'email' => 'john.doe.mailinator.com',
'feedback' => 'whateva',
);
Setup::$front->setRequest(new TestRequest('/feedback/send'));
Setup::dispatch();
$this->assertContains('is not a valid email address', $this->_response->getBody());
}
public function testSendWithBadCaptchaAction()
{
$_POST = array(
'name' => 'john doe',
'email' => 'john.doe@mailinator.com',
'feedback' => 'whateva',
'captcha' => 'whatever',
);
Setup::$front->setRequest(new TestRequest('/feedback/send'));
Setup::dispatch();
$this->assertContains('Captcha value is wrong', $this->_response->getBody());
}
public function testSuccessSendAction()
{
// I gotta render the form first to generate the captcha
$sessionStub = new CaptchaImageTestSessionContainer();
Zend_Registry::set('appSession', $sessionStub);
Setup::$front->setRequest(new TestRequest('/feedback/send'));
Setup::dispatch();
$this->assertEquals(preg_match('/name="captcha\[id\]" value="([0-9a-f]+)"/', $this->_response->__toString(), $matches), 1);
$email = 'john_' . rand(0, 1000) . '@mailinator.com';
$_POST = array(
'name' => 'john',
'email' => $email,
'feedback' => 'whateva',
'captcha' => array(
'input' => CaptchaImageTestSessionContainer::$word,
'id' => $matches[1],
)
);
Setup::$front->setRequest(new TestRequest('/feedback/send'));
Setup::$mockLogger->events = array();
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
// I still don't know how to avoid the "headers already sent" problem here...
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
}
public function testGetMail()
{
require_once APP_DIR . '/modules/default/controllers/FeedbackController.php';
$mail = FeedbackController::getMail('John Black', 'john@mailinator.com', 'whateva');
$this->assertType('Zend_Mail', $mail);
$mailBody = $mail->getBodyText(true);
$mailBody = str_replace("=\n", '', $mailBody); // remove line splitters
$this->assertContains('Dear Administrator', $mailBody);
$this->assertContains('John Black', $mailBody);
$this->assertContains('john@mailinator.com', $mailBody);
$this->assertContains('whateva', $mailBody);
}
public function provideBadFormInput()
{
return array(
array(
'name' => '',
'email' => 'john@mailinator.com',
'feedback' => 'whateva',
),
array(
'name' => 'john doe',
'email' => '',
'feedback' => 'whateva',
),
array(
'name' => 'john doe',
'email' => 'john@mailinator.com',
'feedback' => '',
),
);
}
}

View File

@ -0,0 +1,80 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
class HistoryControllerTests extends PHPUnit_Framework_TestCase
{
private $_response;
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
$users = new Users();
$user = $users->createRow();
$user->id = 23;
$user->role = User::ROLE_ADMIN;
$user->username = 'testuser';
Zend_Registry::set('user', $user);
}
/**
* @expectedException Monkeys_AccessDeniedException
*/
public function testIndexGuestUserAction()
{
Zend_Registry::get('user')->role = User::ROLE_GUEST;
Setup::$front->setRequest(new TestRequest('/history'));
Setup::dispatch();
}
public function testIndexAction()
{
Setup::$front->setRequest(new TestRequest('/history'));
Setup::dispatch();
$this->assertContains('COMMID.history', $this->_response->getBody());
}
public function testListAction()
{
$request = new TestRequest('/history/list?startIndex=0&results=15');
$request->setHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
Setup::$front->setRequest($request);
Setup::dispatch();
$this->assertRegExp(
'#\{("__className":"stdClass",)?"recordsReturned":\d+,"totalRecords":\d+,"startIndex":"\d+",("sort":null,)?"dir":"asc","records":\[.*\]\}#',
$this->_response->getBody()
);
}
/**
* Weak test, till I set up a mock db obj to avoid touching the db
*/
public function testClearAction()
{
$request = new TestRequest('/history/clear');
$request->setHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
Setup::$front->setRequest($request);
Setup::dispatch();
$this->assertRegExp(
'{"code":200}',
$this->_response->getBody()
);
}
}

View File

@ -0,0 +1,50 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
class IdentityControllerTests extends PHPUnit_Framework_TestCase
{
private $_response;
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
// guest user
$users = new Users();
$user = $users->createRow();
Zend_Registry::set('user', $user);
}
/**
* @expectedException Monkeys_BadUrlException
*/
public function testIndexNoIdentityAction()
{
Setup::$front->setRequest(new TestRequest('/identity'));
Setup::dispatch();
}
public function testIdAction()
{
Setup::$front->setRequest(new TestRequest('/identity/whateva'));
$_SERVER['SCRIPT_URI'] = 'http://localhost/communityid/identity/whateva';
Setup::dispatch();
$this->assertContains('<link href="http://localhost/communityid/openid/provider" rel="openid2.provider" />',
$this->_response->getBody());
$this->assertContains('<h2 style="text-align:center">http://localhost/communityid/identity/whateva</h2>',
$this->_response->getBody());
}
}

View File

@ -0,0 +1,139 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
class MessageusersControllerTests extends PHPUnit_Framework_TestCase
{
private $_response;
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
$users = new Users();
$user = $users->createRow();
$user->id = 23;
$user->role = User::ROLE_ADMIN;
$user->username = 'testadmin';
Zend_Registry::set('user', $user);
}
/**
* @expectedException Monkeys_AccessDeniedException
*/
public function testIndexGuestUserAction()
{
Zend_Registry::get('user')->role = User::ROLE_GUEST;
Setup::$front->setRequest(new TestRequest('/messageusers'));
Setup::dispatch();
}
/**
* @expectedException Monkeys_AccessDeniedException
*/
public function testIndexRegisteredUserAction()
{
Zend_Registry::get('user')->role = User::ROLE_REGISTERED;
Setup::$front->setRequest(new TestRequest('/messageusers'));
Setup::dispatch();
}
public function testIndexAction()
{
Setup::$front->setRequest(new TestRequest('/messageusers'));
Setup::dispatch();
$this->assertContains('</form>', $this->_response->getBody());
}
public function testSaveActionWithEmptySubject()
{
$_POST = array(
'messageType' => 'rich',
'subject' => '',
'cc' => '',
'bodyPlain' => '',
'bodyHTML' => 'Hello <strong>world</strong>',
);
Setup::$front->setRequest(new TestRequest('/messageusers/send'));
Setup::dispatch();
$this->assertContains('Value is empty, but a non-empty value is required', $this->_response->getBody());
}
public function testSaveActionWithBadCC()
{
$_POST = array(
'messageType' => 'rich',
'subject' => 'whateva',
'cc' => 'asdfdf',
'bodyPlain' => '',
'bodyHTML' => 'Hello <strong>world</strong>',
);
Setup::$front->setRequest(new TestRequest('/messageusers/send'));
Setup::dispatch();
$this->assertContains('CC field must be a comma-separated list of valid E-mails', $this->_response->getBody());
}
/**
* @expectedException Monkeys_AccessDeniedException
*/
public function testSaveGuestUser()
{
Zend_Registry::get('user')->role = User::ROLE_GUEST;
Setup::$front->setRequest(new TestRequest('/messageusers/send'));
Setup::dispatch();
}
/**
* @expectedException Monkeys_AccessDeniedException
*/
public function testSaveRegisteredUser()
{
Zend_Registry::get('user')->role = User::ROLE_REGISTERED;
Setup::$front->setRequest(new TestRequest('/messageusers/send'));
Setup::dispatch();
}
public function testSaveSuccessfull()
{
$_POST = array(
'messageType' => 'rich',
'subject' => 'whateva',
'cc' => 'one@mailinator.com, two@mailinator.com',
'bodyPlain' => '',
'bodyHTML' => 'Hello <strong>world</strong>',
);
Setup::$front->setRequest(new TestRequest('/messageusers/send'));
Setup::$mockLogger->events = array();
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
// I still don't know how to avoid the "headers already sent" problem here...
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
}
}

View File

@ -0,0 +1,403 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
class OpenidControllerTests extends PHPUnit_Framework_TestCase
{
const CHECKID_QUERY = 'openid.ns=http%%3A%%2F%%2Fspecs.openid.net%%2Fauth%%2F2.0&openid.mode=checkid_setup&openid.identity=http%%3A%%2F%%2Flocalhost%%2Fcommunityid%%2Fidentity%%2Ftestuser&openid.claimed_id=http%%3A%%2F%%2Flocalhost%%2Fcommunityid%%2Fidentity%%2Ftestuser&openid.assoc_handle=%s&openid.return_to=http%%3A%%2F%%2Fwww%%2Eexample%%2Ecom&openid%%2Erealm=http%%3A%%2F%%2Fwww%%2Eexample%%2Ecom';
private $_response;
private $_tempDir;
private $_user;
// state isn't preserved accross test methods, so gotta use a static
public static $assocHandle;
public function __construct($name = NULL, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->_tempDir = APP_DIR . '/tests/temp';
}
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
$users = new Users();
$this->_user = $users->createRow();
$this->_user->test = 1;
$this->_user->username = 'testuser';
$this->_user->role = User::ROLE_REGISTERED;
$this->_user->openid = 'http://localhost/communityid/identity/'.$this->_user->username;
$this->_user->accepted_eula = 1;
$this->_user->firstname = 'firstnametest';
$this->_user->lastname = 'lastnametest';
$this->_user->email = 'usertest@mailinator.com';
$this->_user->token = '';
$this->_user->save();
Zend_Registry::set('user', $this->_user);
}
/**
* @expectedException Monkeys_BadUrlException
*/
public function testIndexAction()
{
Setup::$front->setRequest(new TestRequest('/openid'));
Setup::dispatch();
}
public function testProviderAssociateAction()
{
$_POST = array(
'openid_ns' => 'http://specs.openid.net/auth/2.0',
'openid_mode' => 'associate',
'openid_assoc_type' => 'HMAC-SHA256',
'openid_session_type' => 'DH-SHA256',
'openid_dh_modulus' => 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr',
'openid_dh_gen' => 'Ag==',
'openid_dh_consumer_public' => 'MFzHUMsSa4YSQ3JrcPSqyUaTQ3Z+QWKH6knvrREW7b6zQ2qMdOrpckgnUgo0pILMQpls8Ty/3JDv+IO29qASk2PwwZwxC2kXK/MQC/om5gs/IpjPSw1wK4bz2QTUHTRSxmtTxiq0tHYmIIqadz4TTMfXohMU2VCuYBqDNMHZFpk=',
);
// needed by Zend_OpenId_Provider
$_SERVER["REQUEST_METHOD"] = 'POST';
Setup::$front->setRequest(new TestRequest('/openid/provider'));
Setup::dispatch();
$this->assertEquals(
preg_match(
"%
ns:http://specs\.openid\.net/auth/2\.0\\x0A
assoc_type:HMAC-SHA256\\x0A
session_type:DH-SHA256\\x0A
dh_server_public:.*\\x0A
enc_mac_key:.*\\x0A
assoc_handle:([a-f0-9]+)\\x0A
expires_in:3600\\x0A
%x",
$this->_response->getBody(),
$matches
),
1
);
self::$assocHandle = $matches[1];
}
public function testProviderCheckidSetupAction()
{
// needed by Zend_OpenId_Provider
$_SERVER["REQUEST_METHOD"] = 'GET';
Zend_OpenId::$exitOnRedirect = false;
Setup::$front->setRequest(new TestRequest('/openid/provider?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser&openid.claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser&openid.assoc_handle='.self::$assocHandle.'&openid.return_to=http%3A%2F%2Fwww%2Eexample%2Ecom&openid.realm=http%3A%2F%2Fwww%2Eexample%2Ecom'));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://.*/communityid/openid/login\?
openid\.ns=http%3A%2F%2Fspecs\.openid\.net%2Fauth%2F2\.0
&openid\.mode=checkid_setup
&openid\.identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid\.claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid\.assoc_handle='.self::$assocHandle.'
&openid\.return_to=http%3A%2F%2Fwww\.example\.com
&openid\.realm=http%3A%2F%2Fwww\.example\.com
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function testLoginAction()
{
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
Setup::$front->setRequest(new TestRequest('/openid/login?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertContains('<form action="authenticate?'.$_SERVER['QUERY_STRING'].'" method="post">', $this->_response->getBody());
}
public function testAuthenticateEmptyUsernameAction()
{
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
$_POST = array(
'openIdIdentity' => '',
'password' => 'whateva',
);
Setup::$front->setRequest(new TestRequest('/openid/authenticate?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertContains('Value is empty, but a non-empty value is required', $this->_response->getBody());
}
public function testAuthenticateBadUsernameAction()
{
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
$_POST = array(
'openIdIdentity' => 'whateva',
'password' => 'whatevaagain',
);
Zend_OpenId::$exitOnRedirect = false;
Setup::$front->setRequest(new TestRequest('/openid/authenticate?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://.*/communityid/openid/provider\?
openid_ns=http%3A%2F%2Fspecs\.openid\.net%2Fauth%2F2\.0
&openid_mode=checkid_setup
&openid_identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid_claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid_assoc_handle='.self::$assocHandle.'
&openid_return_to=http%3A%2F%2Fwww\.example\.com
&openid_realm=http%3A%2F%2Fwww\.example\.com
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function testAuthenticateBadPasswordAction()
{
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
$_POST = array(
'openIdIdentity' => $this->_user->openid,
'password' => 'whateva',
);
Zend_OpenId::$exitOnRedirect = false;
Setup::$front->setRequest(new TestRequest('/openid/authenticate?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://.*/communityid/openid/provider\?
openid_ns=http%3A%2F%2Fspecs\.openid\.net%2Fauth%2F2\.0
&openid_mode=checkid_setup
&openid_identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid_claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid_assoc_handle='.self::$assocHandle.'
&openid_return_to=http%3A%2F%2Fwww\.example\.com
&openid_realm=http%3A%2F%2Fwww\.example\.com
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function testAuthenticateSuccessfulAction()
{
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
$_POST = array(
'openIdIdentity' => $this->_user->openid,
'password' => 'm',
);
Zend_OpenId::$exitOnRedirect = false;
Setup::$front->setRequest(new TestRequest('/openid/authenticate?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://.*/communityid/openid/provider\?
openid_ns=http%3A%2F%2Fspecs\.openid\.net%2Fauth%2F2\.0
&openid_mode=checkid_setup
&openid_identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid_claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid_assoc_handle='.self::$assocHandle.'
&openid_return_to=http%3A%2F%2Fwww\.example\.com
&openid_realm=http%3A%2F%2Fwww\.example\.com
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function testTrustAction1()
{
$openIdUser = new OpenIdUser();
$openIdUser->setLoggedInUser($this->_user->openid);
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
// needed by Zend_OpenId_Provider
$_SERVER["REQUEST_METHOD"] = 'GET';
Zend_OpenId::$exitOnRedirect = false;
Setup::$front->setRequest(new TestRequest('/openid/provider?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://.*/communityid/openid/trust\?
openid.ns=http%3A%2F%2Fspecs\.openid\.net%2Fauth%2F2\.0
&openid.mode=checkid_setup
&openid.identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid.claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid.assoc_handle='.self::$assocHandle.'
&openid.return_to=http%3A%2F%2Fwww\.example\.com
&openid.realm=http%3A%2F%2Fwww\.example\.com
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function testTrustAction2()
{
$openIdUser = new OpenIdUser();
$openIdUser->setLoggedInUser($this->_user->openid);
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
Setup::$front->setRequest(new TestRequest('/openid/trust?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertContains(
'A site identifying as <a href="http://www.example.com/">http://www.example.com/</a> has asked for confirmation that <a href="'.$this->_user->openid.'">'.$this->_user->openid.'</a> is your identity URL.',
$this->_response->getBody()
);
}
public function testProviderProceedAction()
{
$openIdUser = new OpenIdUser();
$openIdUser->setLoggedInUser($this->_user->openid);
$_SERVER['QUERY_STRING'] = sprintf(self::CHECKID_QUERY, self::$assocHandle);
// required for logging
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
Zend_OpenId::$exitOnRedirect = false;
$_POST = array(
'action' => 'proceed',
'allow' => 'Allow',
);
Setup::$front->setRequest(new TestRequest('/openid/provider?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://www.example.com\?
openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.assoc_handle='.self::$assocHandle.'
&openid.return_to=http%3A%2F%2Fwww.example.com
&openid.claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid.identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid.op_endpoint=http%3A%2F%2F.*
&openid.response_nonce='.gmdate('Y-m-d\T').'.*
&openid.mode=id_res
&openid.signed=ns%2Cassoc_handle%2Creturn_to%2Cclaimed_id%2Cidentity%2Cop_endpoint%2Cresponse_nonce%2Cmode%2Csigned
&openid.sig=.*
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function testAlreadyTrustedWithSreg()
{
$sregData = array(
'nickname' => 'nicktest',
'email' => 'test_x@mailinator.com',
'fullname' => 'Michael Jordan',
);
$sreg = new Zend_OpenId_Extension_Sreg($sregData);
$storage = new Monkeys_OpenId_Provider_Storage_Database();
$storage->addSite($this->_user->openid, 'http://www.example.com', array('Zend_OpenId_Extension_Sreg' => $sregData));
$openIdUser = new OpenIdUser();
$openIdUser->setLoggedInUser($this->_user->openid);
$queryString = self::CHECKID_QUERY . "&openid.ns.sreg=http%%3A%%2F%%2Fopenid.net%%2Fextensions%%2Fsreg%%2F1.1&openid.sreg.required=nickname&openid.sreg.optional=email%%2Cfullname";
$_SERVER['QUERY_STRING'] = sprintf($queryString, self::$assocHandle);
// required for logging
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// needed by Zend_OpenId_Provider
$_SERVER["REQUEST_METHOD"] = 'GET';
Zend_OpenId::$exitOnRedirect = false;
Setup::$front->setRequest(new TestRequest('/openid/provider?' . $_SERVER['QUERY_STRING']));
Setup::dispatch();
$this->assertEquals(
preg_match(
'#
<script\ language="JavaScript"\ type="text/javascript">window\.location=\'http://www.example.com\?
openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.assoc_handle='.self::$assocHandle.'
&openid.return_to=http%3A%2F%2Fwww.example.com
&openid.claimed_id=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid.identity=http%3A%2F%2Flocalhost%2Fcommunityid%2Fidentity%2Ftestuser
&openid.op_endpoint=http%3A%2F%2F.*
&openid.response_nonce='.gmdate('Y-m-d\T').'.*
&openid.mode=id_res
&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1
&openid.sreg.nickname=nicktest
&openid.sreg.email=test_x%40mailinator.com
&openid.sreg.fullname=Michael\+Jordan
&openid.signed=ns%2Cassoc_handle%2Creturn_to%2Cclaimed_id%2Cidentity%2Cop_endpoint%2Cresponse_nonce%2Cmode%2Cns.sreg%2Csreg.nickname%2Csreg.email%2Csreg.fullname%2Csigned
&openid.sig=.*
\';</script>
#x',
$this->_response->getBody()
),
1
);
}
public function tearDown()
{
$users = new Users();
$this->_user->delete();
}
}

View File

@ -0,0 +1,52 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkeys Ltd.
* @since Textroller 0.9
* @package TextRoller
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
class Users_ProfilegeneralControllerTests extends PHPUnit_Framework_TestCase
{
private $_response;
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
}
public function testChangepasswordAction()
{
$users = new Users();
$user = $users->createRow();
$user->id = 23;
$user->role = User::ROLE_REGISTERED;
Zend_Registry::set('user', $user);
$targetUser = $users->createRow();
$targetUser->id = 24;
Zend_Registry::set('targetUser', $targetUser);
Setup::$front->setRequest(new TestRequest('/users/profilegeneral/changepassword'));
try {
Setup::dispatch();
$this->fail();
} catch (Exception $e) {
$this->assertType('Monkeys_AccessDeniedException', $e);
}
$targetUser = clone $user;
Zend_Registry::set('targetUser', $targetUser);
Setup::dispatch();
$this->assertContains('<form name="changePasswordForm"', $this->_response->getBody());
}
}

View File

@ -0,0 +1,311 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkeys Ltd.
* @since Textroller 0.9
* @package TextRoller
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
require_once dirname(__FILE__) . '/../../../CaptchaImageTestSessionContainer.php';
class Users_RegisterControllerTests extends PHPUnit_Framework_TestCase
{
private $_response;
public function setUp()
{
TestHarness::setUp();
Setup::$front->returnResponse(true);
$this->_response = new Zend_Controller_Response_Http();
Setup::$front->setResponse($this->_response);
}
public function testIndexAction()
{
Setup::$front->setRequest(new TestRequest('/users/register'));
Setup::dispatch();
$this->assertContains('</form>', $this->_response->getBody());
}
/**
* @dataProvider provideBadRegistrationInput
*/
public function testSaveActionWithSomeEmptyFields(
$firstname, $lastname, $email, $username, $password1, $password2
)
{
$_POST = array(
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $email,
'username' => $username,
'password1' => $password1,
'password2' => $password2,
);
Setup::$front->setRequest(new TestRequest('/users/register/save'));
Setup::dispatch();
$this->assertContains('Value is empty, but a non-empty value is required', $this->_response->getBody());
}
public function testSaveActionWithBadEmail()
{
$_POST = array(
'firstname' => 'john',
'lastname' => 'smith',
'email' => 'john.mailinator.com',
'username' => 'johns34',
'password1' => 'johns',
'password2' => 'johns',
);
Setup::$front->setRequest(new TestRequest('/users/register/save'));
Setup::dispatch();
$this->assertContains('is not a valid email address', $this->_response->getBody());
}
public function testSaveActionWithUnmatchedPasswords()
{
$_POST = array(
'firstname' => 'john',
'lastname' => 'smith',
'email' => 'john@mailinator.com',
'username' => 'johns34',
'password1' => 'johnsa',
'password2' => 'johns',
);
Setup::$front->setRequest(new TestRequest('/users/register/save'));
Setup::dispatch();
$this->assertContains('Password confirmation does not match', $this->_response->getBody());
}
public function testSaveActionWithBadCaptcha()
{
$_POST = array(
'firstname' => 'john',
'lastname' => 'smith',
'email' => 'john@mailinator.com',
'username' => 'johns34',
'password1' => 'johns',
'password2' => 'johns',
'captcha' => 'whatever',
);
Setup::$front->setRequest(new TestRequest('/users/register/save'));
Setup::dispatch();
$this->assertContains('Captcha value is wrong', $this->_response->getBody());
}
public function testSuccessfullSaveAction()
{
// I gotta render the form first to generate the captcha
$sessionStub = new CaptchaImageTestSessionContainer();
Zend_Registry::set('appSession', $sessionStub);
Setup::$front->setRequest(new TestRequest('/users/register'));
Setup::dispatch();
$this->assertEquals(preg_match('/name="captcha\[id\]" value="([0-9a-f]+)"/', $this->_response->__toString(), $matches), 1);
$email = 'john_' . rand(0, 1000) . '@mailinator.com';
$_POST = array(
'firstname' => 'john',
'lastname' => 'smith',
'email' => $email,
'username' => 'johns34',
'password1' => 'johns',
'password2' => 'johns',
'captcha' => array(
'input' => CaptchaImageTestSessionContainer::$word,
'id' => $matches[1],
)
);
// this is used to build the users's openid URL
$_SERVER['SCRIPT_URI'] = 'http://localhost/communityid/users/register/save';
Setup::$front->setRequest(new TestRequest('/users/register/save'));
Setup::$mockLogger->events = array();
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
// I still don't know how to avoid the "headers already sent" problem here...
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
$users = new Users();
$user = $users->getUserWithEmail($email);
$this->assertType('User', $user);
$this->assertEquals('johns34', $user->username);
$this->assertEquals('http://localhost/communityid/identity/johns34', $user->openid);
$this->assertEquals(0, $user->accepted_eula);
$this->assertEquals('john', $user->firstname);
$this->assertEquals('smith', $user->lastname);
$this->assertEquals($email, $user->email);
$this->assertEquals(User::ROLE_GUEST, $user->role);
$this->assertNotEquals('', $user->token);
$user->delete();
}
public function testGetMail()
{
$user = $this->_getUser();
// this is used to build the the registration URL
$_SERVER['SCRIPT_URI'] = 'http://localhost/communityid/users/register/save';
require_once APP_DIR . '/modules/users/controllers/RegisterController.php';
$mail = Users_RegisterController::getMail($user);
$this->assertType('Zend_Mail', $mail);
$mailBody = $mail->getBodyText(true);
$mailBody = str_replace("=\n", '', $mailBody); // remove line splitters
$this->assertContains('Dear ' . $user->getFullName(), $mailBody);
$this->assertEquals(preg_match('#http://localhost/communityid/users/register/eula\?token=3D([0-9a-f=\n]+)#', $mailBody, $matches), 1);
$token = str_replace('=0', '', $matches[1]); // remove trailing return chars
$token = str_replace(array('=', "\n"), '', $token);
$this->assertEquals($token, $user->token);
}
public function testEulaBadTokenAction()
{
$_GET = array('token' => 'asdfsdf');
Setup::$front->setRequest(new TestRequest('/users/register/eula'));
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
}
public function testEulaAction()
{
$user = $this->_getUser();
$user->save();
$_GET = array('token' => $user->token);
Setup::$front->setRequest(new TestRequest('/users/register/eula'));
Setup::dispatch();
$fp = fopen(dirname(__FILE__) . '/../../../../resources/eula.txt', 'r');
$firstLine = fgets($fp);
$this->assertContains($firstLine, $this->_response->getBody());
$user->delete();
}
public function testDeclineeulaBadTokenAction()
{
$_GET = array('token' => 'asdfsdf');
Setup::$front->setRequest(new TestRequest('/users/register/declineeula'));
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("invalid token", $lastLog['message']);
}
public function testDeclineeulaAction()
{
$user = $this->_getUser();
$user->save();
$token = $user->token;
$_GET = array('token' => $user->token);
Setup::$front->setRequest(new TestRequest('/users/register/declineeula'));
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
$users = new Users();
$user = $users->getUserWithToken($token);
$this->assertNull($user);
}
public function testAccepteulaBadTokenAction()
{
$_GET = array('token' => 'asdfsdf');
Setup::$front->setRequest(new TestRequest('/users/register/accepteula'));
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to ''", $lastLog['message']);
}
public function testAccepteulaAction()
{
$user = $this->_getUser();
$user->save();
$token = $user->token;
$_GET = array('token' => $user->token);
Setup::$front->setRequest(new TestRequest('/users/register/accepteula'));
try {
Setup::dispatch();
} catch (Zend_Controller_Response_Exception $e) {
}
$lastLog = array_pop(Setup::$mockLogger->events);
$this->assertEquals("redirected to '/users/profile'", $lastLog['message']);
$user->delete();
}
public function provideBadRegistrationInput()
{
return array(
array(
'firstname' => '',
'lastname' => 'smith',
'email' => 'john@mailinator.com',
'username' => 'johns34',
'password1' => 'johns',
'password2' => 'johns',
),
array(
'firstname' => 'john',
'lastname' => '',
'email' => 'john@mailinator.com',
'username' => 'johns34',
'password1' => 'johns',
'password2' => 'johns',
),
array(
'firstname' => 'john',
'lastname' => 'smith',
'email' => 'john@mailinator.com',
'username' => 'johns34',
'password1' => '',
'password2' => '',
),
);
}
private function _getUser()
{
$users = new Users();
$user = $users->createRow();
$user->firstname = 'john';
$user->lastname = 'smith';
$user->token = User::generateToken();
$user->email = 'john@mailinator.com';
return $user;
}
}

View File

@ -0,0 +1,57 @@
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkeys Ltd.
* @since Textroller 0.9
* @package TextRoller
* @packager Keyboard Monkeys
*/
require_once dirname(__FILE__) . '/../../../TestHarness.php';
class UsersTests extends PHPUnit_Framework_TestCase
{
public function setUp()
{
TestHarness::setUp();
}
public function testUserCreating()
{
$users = new Users();
$user = $users->getUserWithEmail('thisshouldntexist');
$this->assertNull($user);
$user = $users->createRow();
$user->test = 1;
$user->username = 'usernametest';
$user->openid = 'http://example.com';
$user->accepted_eula = 1;
$user->firstname = 'firstnametest';
$user->lastname = 'lastnametest';
$user->email = 'usertest@mailinator.com';
$user->role = User::ROLE_REGISTERED;
$user->token = '';
$user->save();
$user = $users->getUserWithEmail('usertest@mailinator.com');
$this->assertType('User', $user);
$this->assertEquals('usernametest', $user->username);
$this->assertEquals('http://example.com', $user->openid);
$this->assertEquals(1, $user->accepted_eula);
$this->assertEquals('firstnametest', $user->firstname);
$this->assertEquals('lastnametest', $user->lastname);
$this->assertEquals('usertest@mailinator.com', $user->email);
$this->assertEquals(User::ROLE_REGISTERED, $user->role);
$this->assertEquals('', $user->token);
$user->delete();
$user = $users->getUserWithEmail('thisshouldntexist');
$this->assertNull($user);
}
}