<commit_before><?php
/**
 * li₃: the most RAD framework for PHP (http://li3.me)
 *
 * Copyright 2016, Union of RAD. All rights reserved. This source
 * code is distributed under the terms of the BSD 3-Clause License.
 * The full license text can be found in the LICENSE.txt file.
 */

namespace lithium\tests\cases\storage\session\strategy;

use lithium\storage\session\strategy\Encrypt;
use lithium\tests\mocks\storage\session\strategy\MockCookieSession;

class EncryptTest extends \lithium\test\Unit {

	public $secret = 'foobar';

	/**
	 * Skip the test if the mcrypt extension is unavailable.
	 */
	public function skip() {
		$this->skipIf(!Encrypt::enabled(), 'The Mcrypt extension is not installed or enabled.');
	}

	public function setUp() {
		$this->mock = 'lithium\tests\mocks\storage\session\strategy\MockCookieSession';
		MockCookieSession::reset();
	}

	public function testConstructException() {
		$this->assertException('/Encrypt strategy requires a secret key./', function() {
			new Encrypt();
		});
	}

	public function testEnabled() {
		$this->assertTrue(Encrypt::enabled());
	}

	public function testConstruct() {
		$encrypt = new Encrypt(['secret' => $this->secret]);
		$this->assertInstanceOf('lithium\storage\session\strategy\Encrypt', $encrypt);
	}

	public function testWrite() {
		$encrypt = new Encrypt(['secret' => $this->secret]);

		$key = 'fookey';
		$value = 'barvalue';

		$result = $encrypt->write($value, ['class' => $this->mock, 'key' => $key]);
		$cookie = MockCookieSession::data();

		$this->assertNotEmpty($result);
		$this->assertNotEmpty($cookie['__encrypted']);
		$this->assertInternalType('string', $cookie['__encrypted']);
		$this->assertNotEqual($cookie['__encrypted'], $value);
	}

	public function testRead() {
		$encrypt = new Encrypt(['secret' => $this->secret]);

		$key = 'fookey';
		$value = 'barvalue';

		$result = $encrypt->write($value, ['class' => $this->mock, 'key' => $key]);
		$this->assertNotEmpty($result);

		$cookie = MockCookieSession::data();
		$result = $encrypt->read($key, ['class' => $this->mock, 'key' => $key]);

		$this->assertEqual($value, $result);
		$this->assertNotEqual($cookie['__encrypted'], $result);
	}

	public function testDelete() {
		$encrypt = new Encrypt(['secret' => $this->secret]);

		$key = 'fookey';
		$value = 'barvalue';

		$result = $encrypt->write($value, ['class' => $this->mock, 'key' => $key]);
		$this->assertNotEmpty($result);

		$cookie = MockCookieSession::data();
		$result = $encrypt->read($key, ['class' => $this->mock, 'key' => $key]);

		$this->assertEqual($value, $result);

		$result = $encrypt->delete($key, ['class' => $this->mock, 'key' => $key]);

		$cookie = MockCookieSession::data();
		$this->assertEmpty($cookie['__encrypted']);

		$result = $encrypt->read($key, ['class' => $this->mock]);
		$this->assertEmpty($result);
	}
}

?><commit_msg>Silence mcrypt 7.1 deprecation in test.<commit_after><?php
/**
 * li₃: the most RAD framework for PHP (http://li3.me)
 *
 * Copyright 2016, Union of RAD. All rights reserved. This source
 * code is distributed under the terms of the BSD 3-Clause License.
 * The full license text can be found in the LICENSE.txt file.
 */

namespace lithium\tests\cases\storage\session\strategy;

use lithium\storage\session\strategy\Encrypt;
use lithium\tests\mocks\storage\session\strategy\MockCookieSession;

class EncryptTest extends \lithium\test\Unit {

	public $secret = 'foobar';

	/**
	 * Skip the test if the mcrypt extension is unavailable.
	 */
	public function skip() {
		$this->skipIf(!Encrypt::enabled(), 'The Mcrypt extension is not installed or enabled.');
		 error_reporting(($this->_backup = error_reporting()) & ~E_USER_DEPRECATED);
	}

	public function setUp() {
		$this->mock = 'lithium\tests\mocks\storage\session\strategy\MockCookieSession';
		MockCookieSession::reset();
		error_reporting($this->_backup);
	}

	public function testConstructException() {
		$this->assertException('/Encrypt strategy requires a secret key./', function() {
			new Encrypt();
		});
	}

	public function testEnabled() {
		$this->assertTrue(Encrypt::enabled());
	}

	public function testConstruct() {
		$encrypt = new Encrypt(['secret' => $this->secret]);
		$this->assertInstanceOf('lithium\storage\session\strategy\Encrypt', $encrypt);
	}

	public function testWrite() {
		$encrypt = new Encrypt(['secret' => $this->secret]);

		$key = 'fookey';
		$value = 'barvalue';

		$result = $encrypt->write($value, ['class' => $this->mock, 'key' => $key]);
		$cookie = MockCookieSession::data();

		$this->assertNotEmpty($result);
		$this->assertNotEmpty($cookie['__encrypted']);
		$this->assertInternalType('string', $cookie['__encrypted']);
		$this->assertNotEqual($cookie['__encrypted'], $value);
	}

	public function testRead() {
		$encrypt = new Encrypt(['secret' => $this->secret]);

		$key = 'fookey';
		$value = 'barvalue';

		$result = $encrypt->write($value, ['class' => $this->mock, 'key' => $key]);
		$this->assertNotEmpty($result);

		$cookie = MockCookieSession::data();
		$result = $encrypt->read($key, ['class' => $this->mock, 'key' => $key]);

		$this->assertEqual($value, $result);
		$this->assertNotEqual($cookie['__encrypted'], $result);
	}

	public function testDelete() {
		$encrypt = new Encrypt(['secret' => $this->secret]);

		$key = 'fookey';
		$value = 'barvalue';

		$result = $encrypt->write($value, ['class' => $this->mock, 'key' => $key]);
		$this->assertNotEmpty($result);

		$cookie = MockCookieSession::data();
		$result = $encrypt->read($key, ['class' => $this->mock, 'key' => $key]);

		$this->assertEqual($value, $result);

		$result = $encrypt->delete($key, ['class' => $this->mock, 'key' => $key]);

		$cookie = MockCookieSession::data();
		$this->assertEmpty($cookie['__encrypted']);

		$result = $encrypt->read($key, ['class' => $this->mock]);
		$this->assertEmpty($result);
	}
}

?>
{'fraction_non_alphanumeric': 0.1963493688161037, 'fraction_numerical': 0.002388263391334016, 'mean_word_length': 4.868868868868869, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2886487', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1881, 'n_words': 415}