<commit_before><?php
App::uses('AssetConfig', 'AssetCompress.Lib');
App::uses('AssetCompressHelper', 'AssetCompress.View/Helper');

App::uses('HtmlHelper', 'View/Helper');
App::uses('View', 'View');


class AssetCompressHelperTest extends CakeTestCase {
/**
 * start a test
 *
 * @return void
 **/
	function setUp() {
		parent::setUp();
		$this->_pluginPath = App::pluginPath('AssetCompress');
		$testFile = $this->_pluginPath . 'Test' . DS . 'test_files' . DS . 'Config' . DS . 'config.ini';

		AssetConfig::clearAllCachedKeys();

		Cache::drop(AssetConfig::CACHE_CONFIG);
		Cache::config(AssetConfig::CACHE_CONFIG, array(
			'path' => TMP,
			'prefix' => 'asset_compress_test_',
			'engine' => 'File'
		));

		$controller = null;
		$request = new CakeRequest(null, false);
		$request->webroot = '';
		$view = new View($controller);
		$view->request = $request;
		$this->Helper = new AssetCompressHelper($view, array('noconfig' => true));
		$Config = AssetConfig::buildFromIniFile($testFile);
		$this->Helper->config($Config);
		$this->Helper->Html = new HtmlHelper($view);

		Router::reload();
		Configure::write('debug', 2);
	}

/**
 * end a test
 *
 * @return void
 **/
	function tearDown() {
		parent::tearDown();
		unset($this->Helper);

		Cache::delete(AssetConfig::CACHE_BUILD_TIME_KEY, AssetConfig::CACHE_CONFIG);
		Cache::drop(AssetConfig::CACHE_CONFIG);
		@unlink(TMP . AssetConfig::BUILD_TIME_FILE);
	}

/**
 * test that assets only have one base path attached
 *
 * @return void
 */
	function testIncludeAssets() {
		Router::setRequestInfo(array(
			array('controller' => 'posts', 'action' => 'index', 'plugin' => null),
			array('base' => '/some/dir', 'webroot' => '/some/dir/', 'here' => '/some/dir/posts')
		));
		$this->Helper->Html->webroot = '/some/dir/';

		$this->Helper->addScript('one.js');
		$result = $this->Helper->includeAssets();
		$this->assertPattern('#"/some/dir/asset_compress#', $result, 'double dir set %s');
	}

/**
 * test that setting $compress = false echos original scripts
 *
 * @return void
 */
	function testNoCompression() {
		$this->Helper->addCss('one', 'lib');
		$this->Helper->addCss('two');
		$this->Helper->addScript('one');
		$this->Helper->addScript('dir/two');

		$result = $this->Helper->includeAssets(true);
		$expected = array(
			array(
				'link' => array(
					'type' => 'text/css',
					'rel' => 'stylesheet',
					'href' => 'preg:/.*css\/one\.css/'
				)
			),
			array(
				'link' => array(
					'type' => 'text/css',
					'rel' => 'stylesheet',
					'href' => 'preg:/.*css\/two\.css/'
				)
			),
			array(
				'script' => array(
					'type' => 'text/javascript',
					'src' => 'preg:/.*js\/one\.js/'
				)
			),
			'/script',
			array(
				'script' => array(
					'type' => 'text/javascript',
					'src' => 'preg:/.*js\/dir\/two\.js/'
				)
			),
			'/script'
		);
		$this->assertTags($result, $expected, true);
	}

/**
 * test css addition
 *
 * @return void
 */
	function testCssOrderPreserving() {
		$this->Helper->addCss('base');
		$this->Helper->addCss('reset');

		$hash = md5('base_reset');

		$result = $this->Helper->includeAssets();
		$expected = array(
			'link' => array(
				'type' => 'text/css',
				'rel' => 'stylesheet',
				'href' => '/asset_compress/assets/get/' . $hash . '.css?file%5B0%5D=base&file%5B1%5D=reset'
			)
		);
		$this->assertTags($result, $expected);
	}

/**
 * test script addition
 *
 * @return void
 */
	function testScriptOrderPreserving() {
		$this->Helper->addScript('libraries');
		$this->Helper->addScript('thing');

		$hash = md5('libraries_thing');

		$result = $this->Helper->includeAssets();
		$expected = array(
			'script' => array(
				'type' => 'text/javascript',
				'src' => '/asset_compress/assets/get/' . $hash . '.js?file%5B0%5D=libraries&file%5B1%5D=thing'
			),
			'/script'
		);
		$this->assertTags($result, $expected);
	}

/**
 * test that magic slug builds work.
 *
 * @return void
 */
	function testScriptMagicSlugs() {
		$this->Helper->addScript('libraries', ':hash-default');
		$this->Helper->addScript('thing', ':hash-default');
		$this->Helper->addScript('jquery.js', ':hash-jquery');
		$this->Helper->addScript('jquery-ui.js', ':hash-jquery');

		$hash1 = md5('libraries_thing');
		$hash2 = md5('jquery.js_jquery-ui.js');

		$result = $this->Helper->includeAssets();
		$expected = array(
			array('script' => array(
				'type' => 'text/javascript',
				'src' => '/asset_compress/assets/get/' . $hash1 . '.js?file%5B0%5D=libraries&file%5B1%5D=thing'
			)),
			'/script',
			array('script' => array(
				'type' => 'text/javascript',
				'src' => '/asset_compress/assets/get/' . $hash2 . '.js?file%5B0%5D=jquery.js&file%5B1%5D=jquery-ui.js'
			)),
			'/script'
		);<commit_msg>Update failing tests.<commit_after><?php
App::uses('AssetConfig', 'AssetCompress.Lib');
App::uses('AssetCompressHelper', 'AssetCompress.View/Helper');

App::uses('HtmlHelper', 'View/Helper');
App::uses('View', 'View');


class AssetCompressHelperTest extends CakeTestCase {
/**
 * start a test
 *
 * @return void
 **/
	function setUp() {
		parent::setUp();
		$this->_pluginPath = App::pluginPath('AssetCompress');
		$testFile = $this->_pluginPath . 'Test' . DS . 'test_files' . DS . 'Config' . DS . 'config.ini';

		AssetConfig::clearAllCachedKeys();

		Cache::drop(AssetConfig::CACHE_CONFIG);
		Cache::config(AssetConfig::CACHE_CONFIG, array(
			'path' => TMP,
			'prefix' => 'asset_compress_test_',
			'engine' => 'File'
		));

		$controller = null;
		$request = new CakeRequest(null, false);
		$request->webroot = '';
		$view = new View($controller);
		$view->request = $request;
		$this->Helper = new AssetCompressHelper($view, array('noconfig' => true));
		$Config = AssetConfig::buildFromIniFile($testFile);
		$this->Helper->config($Config);
		$this->Helper->Html = new HtmlHelper($view);

		Router::reload();
		Configure::write('debug', 2);
	}

/**
 * end a test
 *
 * @return void
 **/
	function tearDown() {
		parent::tearDown();
		unset($this->Helper);

		Cache::delete(AssetConfig::CACHE_BUILD_TIME_KEY, AssetConfig::CACHE_CONFIG);
		Cache::drop(AssetConfig::CACHE_CONFIG);
		@unlink(TMP . AssetConfig::BUILD_TIME_FILE);
	}

/**
 * test that assets only have one base path attached
 *
 * @return void
 */
	function testIncludeAssets() {
		Router::setRequestInfo(array(
			array('controller' => 'posts', 'action' => 'index', 'plugin' => null),
			array('base' => '/some/dir', 'webroot' => '/some/dir/', 'here' => '/some/dir/posts')
		));
		$this->Helper->Html->webroot = '/some/dir/';

		$this->Helper->addScript('one.js');
		$result = $this->Helper->includeAssets();
		$this->assertPattern('#"/some/dir/asset_compress#', $result, 'double dir set %s');
	}

/**
 * test that setting $compress = false echos original scripts
 *
 * @return void
 */
	function testNoCompression() {
		$this->Helper->addCss('one', 'lib');
		$this->Helper->addCss('two');
		$this->Helper->addScript('one');
		$this->Helper->addScript('dir/two');

		$result = $this->Helper->includeAssets(true);
		$expected = array(
			array(
				'link' => array(
					'type' => 'text/css',
					'rel' => 'stylesheet',
					'href' => 'preg:/.*css\/one\.css/'
				)
			),
			array(
				'link' => array(
					'type' => 'text/css',
					'rel' => 'stylesheet',
					'href' => 'preg:/.*css\/two\.css/'
				)
			),
			array(
				'script' => array(
					'type' => 'text/javascript',
					'src' => 'preg:/.*js\/one\.js/'
				)
			),
			'/script',
			array(
				'script' => array(
					'type' => 'text/javascript',
					'src' => 'preg:/.*js\/dir\/two\.js/'
				)
			),
			'/script'
		);
		$this->assertTags($result, $expected, true);
	}

/**
 * test css addition
 *
 * @return void
 */
	function testCssOrderPreserving() {
		$this->Helper->addCss('base');
		$this->Helper->addCss('reset');

		$hash = md5('base_reset');

		$result = $this->Helper->includeAssets();
		$expected = array(
			'link' => array(
				'type' => 'text/css',
				'rel' => 'stylesheet',
				'href' => '/asset_compress/assets/get/' . $hash . '.css?file%5B0%5D=base&amp;file%5B1%5D=reset'
			)
		);
		$this->assertTags($result, $expected);
	}

/**
 * test script addition
 *
 * @return void
 */
	function testScriptOrderPreserving() {
		$this->Helper->addScript('libraries');
		$this->Helper->addScript('thing');

		$hash = md5('libraries_thing');

		$result = $this->Helper->includeAssets();
		$expected = array(
			'script' => array(
				'type' => 'text/javascript',
				'src' => '/asset_compress/assets/get/' . $hash . '.js?file%5B0%5D=libraries&amp;file%5B1%5D=thing'
			),
			'/script'
		);
		$this->assertTags($result, $expected);
	}

/**
 * test that magic slug builds work.
 *
 * @return void
 */
	function testScriptMagicSlugs() {
		$this->Helper->addScript('libraries', ':hash-default');
		$this->Helper->addScript('thing', ':hash-default');
		$this->Helper->addScript('jquery.js', ':hash-jquery');
		$this->Helper->addScript('jquery-ui.js', ':hash-jquery');

		$hash1 = md5('libraries_thing');
		$hash2 = md5('jquery.js_jquery-ui.js');

		$result = $this->Helper->includeAssets();
		$expected = array(
			array('script' => array(
				'type' => 'text/javascript',
				'src' => '/asset_compress/assets/get/' . $hash1 . '.js?file%5B0%5D=libraries&amp;file%5B1%5D=thing'
			)),
			'/script',
			array('script' => array(
				'type' => 'text/javascript',
				'src' => '/asset_compress/assets/get/' . $hash2 . '.js?file%5B0%5D=jquery.js&amp;file%5B1%5D=jquery-ui.js'
			)),
			'/script'
		);
{'fraction_non_alphanumeric': 0.22327110733983876, 'fraction_numerical': 0.007000424268137463, 'mean_word_length': 3.805810397553517, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 183, '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': '6098702', 'n_tokens_mistral': 3853, 'n_tokens_neox': 3362, 'n_words': 550}