<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class RealURL2_RealURL2Test extends RealURL2_BaseTest {
	public function testGetDomains() {
		$db = sly_DB_Persistence::getInstance();
		$db->update('article', array('wv24_fragment' => '//x.com'),        array('id' => 99, 'clang' => 5));
		$db->update('article', array('wv24_fragment' => '//y.com'),        array('id' => 99, 'clang' => 8));
		$db->update('article', array('wv24_fragment' => '//cli'),          array('id' => 90, 'clang' => 8));
		$db->update('article', array('wv24_fragment' => '//sally/path'),   array('id' => 50, 'clang' => 8));
		$db->update('article', array('wv24_fragment' => '//1.2.3.4/path'), array('id' => 76, 'clang' => 8));

		$domains = WV24_RealURL2::getDomains();
		sort($domains);

		$this->assertEquals(array('1.2.3.4', 'cli', 'sally', 'x.com', 'y.com'), $domains);
	}

	public function testClearCache() {
		sly_Core::cache()->set('realurl2', 'foo', 1);
		sly_Core::cache()->set('realurl2.bar', 'foo', 1);

		WV24_RealURL2::clearCache();

		$this->assertNull(sly_Core::cache()->get('realurl2', 'foo'));
		$this->assertNull(sly_Core::cache()->get('realurl2.bar', 'foo'));
	}

	public function testGetBaseTag() {
		WV24_RealURL2::enableAutoBaseTag();

		$this->assertEquals('<base href="http://cli/sally/">',   WV24_RealURL2::getBaseTag(false, false));
		$this->assertEquals('<base href="http://cli/sally/" />', WV24_RealURL2::getBaseTag(true, false));
		$this->assertTrue(WV24_RealURL2::doBaseTag());

		$this->assertEquals('<base href="http://cli/sally/" />', WV24_RealURL2::getBaseTag());
		$this->assertFalse(WV24_RealURL2::doBaseTag());

		WV24_RealURL2::enableAutoBaseTag();
	}

	public function testDecodeURL() {
		// rebuild all URLs
		self::rebuildAll();

		// does the method work at all?
		$url = WV24_RealURL2::decodeURL('http://cli/');
		$this->assertInstanceof('_WV24_URL', $url);
		$this->assertEquals('http://cli/', $url->getRawURL());

		// are the regular sanatizers working?
		$url = WV24_RealURL2::decodeURL('http://cli/index.html');
		$this->assertInstanceof('_WV24_URL', $url);
		$this->assertEquals('http://cli/', $url->getRawURL());

		// bad URLs should yield null
		$url = WV24_RealURL2::decodeURL('http://cli/34h2kr');
		$this->assertNull($url);
	}
}
