<commit_before><?php
/**
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @copyright     Copyright 2013, <NAME> (http://frankfoerster.com)
 * @link          http://github.com/frankfoerster/wasabi
 * @package       Wasabi
 * @subpackage    Wasabi.Plugin.Core.Test.Case.Controller
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('ControllerTestCase', 'TestSuite');
App::uses('UsersController', 'Core.Controller');

class UsersTestController extends UsersController {

	public $redirectUrl;

	public function redirect($url, $status = null, $exit = true) {
		$this->redirectUrl = $url;
		return false;
	}

}

/**
 * @property UsersTestController $UsersController
 */

class UsersControllerTest extends ControllerTestCase {

	public $fixtures = array('plugin.core.user', 'plugin.core.group', 'plugin.core.language');

	public function setUp() {
		$this->UsersController = new UsersTestController();
		$this->UsersController->constructClasses();
		$this->UsersController->Components->init($this->UsersController);
		$this->UsersController->viewVars['backend_menu_for_layout'] = array(
			'primary' => array(
				array(
					'url' => array(
						'plugin' => 'core',
						'controller' => 'users',
						'action' => 'index'
					)
				)
			)
		);

		parent::setUp();
	}

	public function testRequiredModelsAreSetup() {
		$this->assertTrue(in_array('Core.User', $this->UsersController->uses));
	}

	public function testIndexAction() {
		$this->UsersController->index();
		$this->assertTrue(isset($this->UsersController->viewVars['users']));

		$expected = array(
			array(
				'User' => array(
					'id' => 1,
					'group_id' => 1,
					'language_id' => 1,
					'username' => 'admin',
					'password' => <PASSWORD>',
					'active' => true,
					'created' => '2013-01-12 14:00:00',
					'modified' => '2013-01-12 14:00:00'
				),
				'Group' => array(
					'id' => 1,
					'name' => 'Administrator',
					'user_count' => 2,
					'created' => '2013-01-12 14:00:00',
					'modified' => '2013-01-12 14:00:00'
				)
			),
			array(
				'User' => array(
					'id' => 2,
					'group_id' => 1,
					'language_id' => 1,
					'username' => 'test',
					'password' => <PASSWORD>',
					'active' => false,
					'created' => '2013-01-12 15:00:00',
					'modified' => '2013-01-12 15:00:00'
				),
				'Group' => array(
					'id' => 1,
					'name' => 'Administrator',
					'user_count' => 2,
					'created' => '2013-01-12 14:00:00',
					'modified' => '2013-01-12 14:00:00'
				)
			)
		);
		$result = $this->UsersController->viewVars['users'];
		$this->assertEqual($expected, $result);
	}

	public function testLoginAction() {
		$this->UsersController->data = array();
		$this->UsersController->login();
		$expected = 'Core.login';
		$result = $this->UsersController->layout;
		$this->assertEqual($expected, $result);
		$this->assertTrue(isset($this->UsersController->viewVars['title_for_layout']));
		$this->assertFalse(isset($this->UsersController->viewVars['login_referer']));

		$this->UsersController->Session->write('login_referer', 'backend/users');
		$this->UsersController->data = array();
		$this->UsersController->login();
		$this->assertTrue(isset($this->UsersController->viewVars['login_referer']));
		$expected = 'backend/users';
		$result = $this->UsersController->viewVars['login_referer'];
		$this->assertEqual($expected, $result);
		$this->assertFalse($this->UsersController->Session->check('login_referer'));

		$this->UsersController->data = array(
			'User' => array(
				'username' => 'admin',
				'password' => '<PASSWORD>'
			)
		);
		$this->UsersController->login();
		$this->assertTrue($this->UsersController->Session->check('Message.flash'));
		$expected = 'success';
		$result = $this->UsersController->Session->read('Message.flash.params.class');
		$this->assertEqual($expected, $result);
		$expected = array(
			'plugin' => 'core',
			'controller' => 'users',
			'action' => 'index'
		);
		$result = $this->UsersController->redirectUrl;
		$this->assertEqual($expected, $result);
		$this->UsersController->Session->delete('wasabi');

		$this->UsersController->data = array(
			'User' => array(
				'username' => 'admin',
				'password' => '<PASSWORD>',
				'login_referer' => 'protected/url'
			)
		);
		$this->UsersController->login();
		$expected = '/protected/url';
		$result = $this->UsersController->redirectUrl;
		$this->assertEqual($expected, $result);
		$this->UsersController->Session->delete('wasabi');

		$this->UsersController->data = array(
			'User' => array(
				'username' => 'admin',
				'password' => '<PASSWORD>'
			)
		);
		$this->UsersController->login();
		$this->assertTrue($this->UsersController->Session->check('Message.flash'));
		$expected = 'error';
		$result = $this->UsersController->Session->read('Message.flash.params.class');
		$this->assertEqual($expected, $result);

		$this->UsersController->Session->delete('wasabi');
	}

	public function testLogout() {
		$this->UsersController->logout();

		$this->assertTrue($this->UsersController->Session->check('Message.flash'));

		$expected = 'success';
		$result = $this->UsersController->Session->read('Message.flash.params.class');
		$this->assertEqual($expected, $result);

		$expected = array(
			'action' => 'login'
		);
		$result = $this->UsersController->redirectUrl;
		$this->assertEqual($expected, $result);
	}

	public function tearDown() {
		unset($this->UsersController);

		parent::tearDown();
	}

}<commit_msg>completely refactored, add missing tests, 100% code coverage<commit_after><?php
/**
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @copyright     Copyright 2013, <NAME> (http://frankfoerster.com)
 * @link          http://github.com/frankfoerster/wasabi
 * @package       Wasabi
 * @subpackage    Wasabi.Plugin.Core.Test.Case.Controller
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('CoreControllerTest', 'Core.Test/TestSuite');
App::uses('UsersController', 'Core.Controller');

class UsersTestController extends UsersController {

	public $redirectUrl;

	public $renderView;

	public function redirect($url, $status = null, $exit = true) {
		$this->redirectUrl = $url;
	}

	public function prepareAddEdit() {
		$this->_prepareAddEdit();
	}

	public function render($view) {
		$this->renderView = $view;
	}

}

/**
 * @property UsersTestController $Users
 * @method testAction
 */

class UsersControllerTest extends CoreControllerTest {

	public $fixtures = array('plugin.core.user', 'plugin.core.group', 'plugin.core.language', 'plugin.core.login_token');

	public function setUp() {
		$this->Users = $this->generate('UsersTest');

		parent::setUp();
	}

	public function tearDown() {
		unset($this->Users);

		parent::tearDown();
	}

	public function testRequiredModelsAreSetup() {
		$this->assertTrue(in_array('Core.User', $this->Users->uses));
	}

	public function testIndexAction() {
		$this->testAction('/' . $this->backendPrefix . '/users', array('method' => 'get'));

		$this->assertInternalType('string', $this->Users->viewVars['title_for_layout']);
		$this->assertTrue(isset($this->Users->viewVars['users']));
		$this->assertNotEmpty($this->Users->viewVars['users']);
	}

	public function testAddActionGet() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/add', array('method' => 'get'));

		$this->assertNull($this->Users->redirectUrl);
	}

	public function testAddActionPostWithInvalidData() {
		$this->_loginUser();

		$user_count = $this->Users->User->find('count');

		$this->testAction('/' . $this->backendPrefix . '/users/add', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'username' => 'test user',
					'password_unencrypted' => '<PASSWORD>',
					'password_confirmation' => '<PASSWORD>',
					'group_id' => 1,
					'language_id' => 1
				)
			)
		));

		$this->assertEqual($user_count, $this->Users->User->find('count'));
		$this->assertFalse($this->Users->User->hasAny(array('username' => 'test user')));
		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertNull($this->Users->redirectUrl);
	}

	public function testAddActionPost() {
		$this->_loginUser();

		$user_count = $this->Users->User->find('count');

		$this->testAction('/' . $this->backendPrefix . '/users/add', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'username' => 'test user',
					'password_unencrypted' => '<PASSWORD>',
					'password_confirmation' => '<PASSWORD>',
					'group_id' => 1,
					'language_id' => 1
				)
			)
		));

		$this->assertEqual(($user_count + 1), $this->Users->User->find('count'));
		$this->assertTrue($this->Users->User->hasAny(array('username' => 'test user')));
		$this->assertEqual('success', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testEditActionWithoutId() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/edit', array('method' => 'get'));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testEditActionDisallowEditOfGlobalAdminByOtherUsers() {
		$this->_loginUser(2);

		$this->testAction('/' . $this->backendPrefix . '/users/edit/1', array('method' => 'get'));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testEditActionGet() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/edit/1', array('method' => 'get'));

		$this->assertEqual($this->Users->request->data, $this->Users->User->findById(1));
		$this->assertFalse(isset($this->Users->User->validate['password_unencrypted']));
		$this->assertFalse(isset($this->Users->User->validate['password_confirmation']));
		$this->assertFalse($this->Users->Session->check('Message.flash'));
		$this->assertEqual('add', $this->Users->renderView);
	}

	public function testEditActionPostWithInvalidData() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/edit/1', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'id' => 1,
					'username' => 'a'
				)
			)
		));

		$this->assertFalse($this->Users->User->hasAny(array('username' => 'a')));
		$this->assertNotEmpty($this->Users->User->validationErrors);
		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertNull($this->Users->redirectUrl);
	}

	public function testEditActionPost() {
		$this->_loginUser();

		$user_count = $this->Users->User->find('count');

		$this->testAction('/' . $this->backendPrefix . '/users/edit/1', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'id' => 1,
					'username' => 'admin modified'
				)
			)
		));

		$this->assertEqual($user_count, $this->Users->User->find('count'));
		$this->assertTrue($this->Users->User->hasAny(array('username' => 'admin modified')));
		$this->assertEqual('success', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testDeleteActionGetThrowsException() {
		$this->_loginUser();

		$this->setExpectedException('MethodNotAllowedException');

		$this->testAction('/' . $this->backendPrefix . '/users/delete', array('method' => 'get'));
	}

	public function testDeleteActionPostNoId() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/delete', array('method' => 'post'));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testDeleteActionUserCannotDeleteOwnAccount() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/delete/1', array('method' => 'post'));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testDeleteActionNonExistentId() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/users/delete/99', array('method' => 'post'));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testDeleteActionPost() {
		$this->_loginUser();

		$user_count = $this->Users->User->find('count');

		$this->testAction('/' . $this->backendPrefix . '/users/delete/2', array('method' => 'post'));

		$this->assertEqual(($user_count - 1), $this->Users->User->find('count'));
		$this->assertEqual('success', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'index'), $this->Users->redirectUrl);
	}

	public function testLoginActionGet() {
		$this->testAction('/' . $this->backendPrefix . '/login', array('method' => 'get'));

		$this->assertEqual('Core.login', $this->Users->layout);
		$this->assertTrue(isset($this->Users->viewVars['title_for_layout']));
		$this->assertFalse($this->Users->Session->check('login_referer'));
	}

	public function testLoginActionGetLoginRefererIsCarriedOn() {
		$this->Users->Session->write('login_referer', '/foo');

		$this->testAction('/' . $this->backendPrefix . '/login', array('method' => 'get'));

		$this->assertTrue($this->Users->Session->check('login_referer'));
	}

	public function testLoginActionGetWithLoggedInUser() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/login', array('method' => 'get'));

		$this->assertFalse($this->Users->Session->check('login_referer'));

		$expected = $this->Users->viewVars['backend_menu_for_layout']['primary'][0]['url'];
		$result = $this->Users->redirectUrl;
		$this->assertEqual($expected, $result);
	}

	public function testLoginActionGetWithLoggedInUserAndCustomLoginReferer() {
		$this->_loginUser();
		$this->Users->Session->write('login_referer', '/foo');

		$this->testAction('/' . $this->backendPrefix . '/login', array('method' => 'get'));

		$this->assertFalse($this->Users->Session->check('login_referer'));
		$this->assertEqual('/foo', $this->Users->redirectUrl);
	}

	public function testLoginActionPostWithoutData() {
		$this->testAction('/' . $this->backendPrefix . '/login', array('method' => 'post'));

		$this->assertTrue($this->Users->Session->check('Message.flash'));
		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertTrue($this->Users->Session->check('login_referer'));
	}

	public function testLoginActionPostWithoutDataAndCustomLoginReferer() {
		$this->Users->Session->write('login_referer', '/foo');

		$this->testAction('/' . $this->backendPrefix . '/login', array('method' => 'post'));

		$this->assertTrue($this->Users->Session->check('Message.flash'));
		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertTrue($this->Users->Session->check('login_referer'));
		$this->assertEqual('/foo', $this->Users->Session->read('login_referer'));
	}

	public function testLoginActionPostWithInvalidForm() {
		$this->testAction('/' . $this->backendPrefix . '/login', array(
			'method' => 'post',
			'data' => array(
				'Foo' => 'bar'
			)
		));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertNull($this->Users->redirectUrl);
	}

	public function testLoginActionPostWithValidFormAndInvalidData() {
		$this->testAction('/' . $this->backendPrefix . '/login', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'username' => 'foo',
					'password' => '<PASSWORD>'
				)
			)
		));

		$this->assertEqual('error', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertNull($this->Users->redirectUrl);
	}

	public function testLoginActionPost() {
		$this->testAction('/' . $this->backendPrefix . '/login', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'username' => 'admin',
					'password' => '<PASSWORD>'
				)
			)
		));

		$this->assertEqual('success', $this->Users->Session->read('Message.flash.params.class'));

		$expected = $this->Users->viewVars['backend_menu_for_layout']['primary'][0]['url'];
		$result = $this->Users->redirectUrl;
		$this->assertEqual($expected, $result);
	}

	public function testLoginActionPostWithCustomLoginReferer() {
		$this->Users->Session->write('login_referer', '/foo');

		$this->testAction('/' . $this->backendPrefix . '/login', array(
			'method' => 'post',
			'data' => array(
				'User' => array(
					'username' => 'admin',
					'password' => '<PASSWORD>'
				)
			)
		));

		$this->assertEqual('success', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual('/foo', $this->Users->redirectUrl);
		$this->assertFalse($this->Users->Session->check('login_referer'));
	}

	public function testLogout() {
		$this->_loginUser();

		$this->testAction('/' . $this->backendPrefix . '/logout', array('method' => 'get'));

		$this->assertEmpty($this->Users->Authenticator->get());
		$this->assertTrue($this->Users->Session->check('Message.flash'));
		$this->assertEqual('success', $this->Users->Session->read('Message.flash.params.class'));
		$this->assertEqual(array('action' => 'login'), $this->Users->redirectUrl);
	}

}
{'fraction_non_alphanumeric': 0.21731309724564363, 'fraction_numerical': 0.008712759977515458, 'mean_word_length': 4.640773620798986, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 595, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4736811', 'n_tokens_mistral': 6379, 'n_tokens_neox': 5638, 'n_words': 835}