Location: PHPKode > scripts > NIKAcore > nikacore/PageName.test.php
<?php

// NIKAcore: core classes for PHP websites
// 
// Copyright (C) 2003 NIKA Consulting, Inc.
//
// This file is part of NIKAcore.
// 
// NIKAcore is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
// 
// NIKAcore is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
// more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with NIKAcore; if not, write to the Free Software Foundation, Inc., 59
// Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Contact hide@address.com with comments, questions, or patches.

require_once('test-setup.php');
require_once('PageName.php');

class FakeCollection {}

class Tst_PageName extends MyTestCase {

	function createCollection() { return new FakeCollection; }

	function createPN($line) {
		return PageName::createFromLine($this->createCollection(), $line);
	}

	function assertLineNull($line, $comment = '') {
		$this->assertNull($this->createPN($line), $comment);
	}

	function test_createFromLine_empty() {
		$this->assertLineNull('', 'Empty string should yield null result.');
		$this->assertLineNull("\n", 'Empty line should yield null result.');
		$this->assertLineNull(" \n", 'Whitespace-only line should yield null result.');
		$this->assertLineNull("\t\n", 'Whitespace-only line should yield null result.');
	}

	function test_createFromLine_simple() {
		$pn = $this->createPN('/=Page Name');
		$this->assertEquals('/', $pn->getUrl(),
			'Simple assignment should yield correct URL.'
		);
		$this->assertEquals('Page Name', $pn->getLongName(),
			'Simple assignment should yield correct long name.'
		);
		$this->assertEquals('Page Name', $pn->getShortName(),
			'Simple assignment should yield correct short name.'
		);
	}

	function test_createFromLine_shortname() {
		$pn = $this->createPN('/=Page Name|short');
		$this->assertEquals('/', $pn->getUrl(),
			'Simple assignment should yield correct URL.'
		);
		$this->assertEquals('Page Name', $pn->getLongName(),
			'Simple assignment should yield correct long name.'
		);
		$this->assertEquals('short', $pn->getShortName(),
			'Simple assignment should yield correct short name.'
		);
	}

	function test_createFromLine_commented_out() {
		$mapping = $this->createPN('#/=Page Name|short');
		$this->assertNull($mapping);
	}

	function test_createFromLine_commented_out_shortname() {
		$mapping = $this->createPN('/index.html=A Name of Page#|short');
		$this->assertEquals('/index.html', $mapping->getUrl(),
			'Simple assignment should yield correct URL.'
		);
		$this->assertEquals('A Name of Page', $mapping->getLongName(),
			'Simple assignment should yield correct long name.'
		);
		$this->assertEquals('A Name of Page', $mapping->getShortName(),
			'Simple assignment should yield correct short name.'
		);
	}

	function test_createFromLine_whitespace_shortname_comment() {
		$mapping = $this->createPN(
			'  /document/path/file   =  Page Name    |   short name 2   # comment'
		);
		$this->assertEquals('/document/path/file', $mapping->getUrl(),
			'should yield correct URL.'
		);
		$this->assertEquals('Page Name', $mapping->getLongName(),
			'should yield correct long name.'
		);
		$this->assertEquals('short name 2', $mapping->getShortName(),
			'should yield correct short name.'
		);
	}

	function test_non_null() {
		$m = $this->createPN('/blorg=You Are Somebody');
		$this->assert(! $m->isNull());
	}

	function test_null() {
		$pn = PageName::newNull();
		$this->assert($pn->isNull());
		$this->assertEquals(null, $pn->getShortName());
		$this->assertEquals(null, $pn->getLongName());
	}

	function test_makes_null() {
		$m = $this->createPN('# blah blah');
		$this->assert($m->isNull());
	}

	function test_knows_collection() {
		$sourceColl = new FakeCollection;
		$pn = PageName::createFromLine($sourceColl, '/=Yo');
		$coll = $pn->getCollection();
		$this->assertClass('FakeCollection', $coll);
	}

	function test_knows_ref_to_collection_not_copy() {
		$sourceColl = new PageNameCollection;
		$this->assertEquals(0, $sourceColl->count());
		$this->assertNull($sourceColl->getPageNameForUrl('/'));
		$pn = PageName::createFromLine($sourceColl, '/=Yo');
		$sourceColl->add($pn);
		$looked_up_pn = $sourceColl->getPageNameForUrl('/');
		$this->assertEquals('Yo', $looked_up_pn->getLongName(), 'should remember pn');
		$coll = $pn->getCollection();
		$this->assertEquals(
			1, $coll->count(), 'pn should remember reference to collection'
		);
		$this->assertClass('PageNameCollection', $coll);
	}
	
	function test_htmlPageTitleTag() {
		$coll = new PageNameCollection;
		$coll->add(PageName::createFromLine($coll, '/=Site name|Site'));
		$coll->add(PageName::createFromLine($coll, '/blorg.html=Your Weblorg'));
		$pn_root = $coll->getPageNameForUrl('/');
		$pn = $coll->getPageNameForUrl('/blorg.html');
		$this->assertEquals("<title>Site name</title>\n", $pn_root->htmlPageTitleTag());
		$this->assertEquals("<title>Site: Your Weblorg</title>\n", $pn->htmlPageTitleTag());
	}
	
	function test_htmlH1HeadlineTag() {
		$coll = new PageNameCollection;
		$coll->add(PageName::createFromLine($coll, '/=Site name|Site'));
		$coll->add(
			PageName::createFromLine($coll, '/blorg.html=Your Weblorg|Blorgaschmord')
		);
		$pn_root = $coll->getPageNameForUrl('/');
		$pn = $coll->getPageNameForUrl('/blorg.html');
		$this->assertEquals('', $pn_root->htmlH1HeadlineTag());
		$this->assertEquals("<h1>Your Weblorg</h1>\n", $pn->htmlH1HeadlineTag());
	}
		
}
$nikacore_suite->addTest(new TestSuite('Tst_PageName'));

?>
Return current item: NIKAcore