<?php

App::uses('BemmelhorTestCase', 'TestSuite');
App::uses('Router', 'Routing');
App::uses('BemmelhorRouter', 'Lib');

class BemmelhorRouterTest extends BemmelhorTestCase {

	public $fixtures = array(
		'setting',
		'type',
		'vocabulary',
		'types_vocabulary',
	);

	public function setUp() {
		parent::setUp();
		// This test case is only valid for 2.2.x series
		$this->skipIf(version_compare(Configure::version(), '2.2.7', '>'));
	}

	public function testContentType() {
		$params = array(
			'url' => array(),
			'controller' => 'nodes',
			'action' => 'index',
			'type' => 'blog',
		);
		$result = Router::reverse($params);
		$this->assertEquals('/nodes/index/type:blog', $result);

		BemmelhorRouter::contentType('blog');
		$result = Router::reverse($params);
		$this->assertEquals('/blog', $result);

		BemmelhorRouter::contentType('page');
		$params = array(
			'url' => array(),
			'controller' => 'nodes',
			'action' => 'index',
			'type' => 'page',
		);
		$result = Router::reverse($params);
		$this->assertEquals('/page', $result);
	}

	public function testRoutableContentTypes() {
		$Type = ClassRegistry::init('Type');
		$type = $Type->create(array(
			'title' => 'Press Release',
			'alias' => 'press-release',
			'description' => '',
		));
		$Type->save($type);
		$type = $Type->findByAlias('press-release');
		BemmelhorRouter::routableContentTypes();

		$params = array(
			'url' => array(),
			'controller' => 'nodes',
			'action' => 'index',
			'type' => 'press-release',
		);
		$result = Router::reverse($params);
		$this->assertEquals('/nodes/index/type:press-release', $result);

		$type['Type']['params'] = 'routes=1';
		$Type->save($type);
		BemmelhorRouter::routableContentTypes();

		$result = Router::reverse($params);
		$this->assertEquals('/press-release', $result);
	}

}