<commit_before><?php
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

/**
 * @group unit
 *
 */
require_once 'lib/wiki/editlib.php';
require_once 'lib/admin/adminlib.php';


class EditLib_ParseToWysiwyg_LinkTest extends TikiTestCase
{
	private $dir = '';  // the unmodifed directory
	private $el = null; // the EditLib
	private $ext1 = 'test_ext1'; // name of the external Wiki 1


	function __construct()
	{
		$this->dir = getcwd();

		// we must set the page regex, otherwise the links get not parsed
		// taken from: 'lib/setup/wiki.php' with  $prefs['wiki_page_regex'] == 'full'
		global $page_regex;
		$page_regex = '([A-Za-z0-9_]|[\x80-\xFF])([\.: A-Za-z0-9_\-]|[\x80-\xFF])*([A-Za-z0-9_]|[\x80-\xFF])';
	}


	function setUp()
	{
		$_SERVER['HTTP_HOST'] = ''; // editlib expects that HTTP_HOST is defined	
		$_SERVER['SERVER_NAME'] = 'myserver'; // the ParserLib expects the servername to be set	

		global $prefs;
		$prefs['feature_sefurl'] = 'n'; // default

		$this->el = new EditLib();

		chdir($this->dir);
		chdir('../../'); // the tiki installation directory
	}


	function tearDown()
	{
		chdir($this->dir);

		/*
		 * remove the external Wikis defined in the tests 
		 */
		global $tikilib;

		$query = 'SELECT `name`, `extwikiId` FROM `tiki_extwiki`';
		$wikis = $tikilib->fetchMap($query);
		$tmp_wikis = array($this->ext1);

		foreach ($tmp_wikis as $w) {
			if (isset($wikis[$w])) {
				$id = $wikis[$w];
				$tikilib->lib('admin')->remove_extwiki($id);
			}
		}
	}	


	/**
	 * Test links to pages of an external Wiki
	 * 
	 * This test is used to detect changes in the parser. Here, the EditLib is not used.
	 * 
	 * Note: Links with an invalid wiki identifier are parsed as regular Wiki page links.
	 */
	function testExternalWiki()
	{

		/*
		 * setup the external wikis and the parser
		 */
		global $tikilib;
		$tikilib->lib('admin')->replace_extwiki(0, 'http://tikiwiki.org/tiki-index.php?page=$page', $this->ext1);
		$p = $tikilib->lib('parser');


		/*
		 * External Wiki 
		 * - page name
		 */
		$inData = "(($this->ext1:Download))" ;		
		$ex = '<a href="http://tikiwiki.org/tiki-index.php?page=Download" class="wiki ext_page test_ext1">Download</a>';
		$out = trim($p->parse_data($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * External Wiki
		 * - page name
		 * - anchor
		 */
		$inData = "(($this->ext1:Download|#LTS_-_the_Long_Term_Support_release))" ;
		$ex = '<a href="http://tikiwiki.org/tiki-index.php?page=Download#LTS_-_the_Long_Term_Support_release" class="wiki ext_page test_ext1">Download</a>';
		$out = trim($p->parse_data($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * External Wiki
		 * - page name
		 * - anchor
		 * - description
		 */
		$inData = "(($this->ext1:Download|#LTS_-_the_Long_Term_Support_release|Download LTS))" ;	
		$ex = '<a href="http://tikiwiki.org/tiki-index.php?page=Download#LTS_-_the_Long_Term_Support_release" class="wiki ext_page test_ext1">Download LTS</a>';
		$out = trim($p->parse_data($inData));
		$this->assertEquals($ex, $out);	
	}


	/**
	 * Test link to anchor within a page
	 */
	function testInPage()
	{

		/*
		 * with description
		 */
		$inData = '[#A_Heading|Link to heading]';		
		$ex = '<a class="wiki" href="#A_Heading" rel="">Link to heading</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$out = preg_replace('/  /', ' ', $out); // the parser writes to many spaces
		$this->assertEquals($ex, $out);


		/*
		 * no description
		 */
		$inData = '[#A_Heading]';		
		$ex = '<a class="wiki" href="#A_Heading" rel="">#A_Heading</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$out = preg_replace('/  /', ' ', $out); // the parser writes to many spaces
		$this->assertEquals($ex, $out);				
	}


	/**
	 * Test link for creating e-mail
	 */
	function testMailTo()
	{

		/*
		 * e-mail
		 */
		$inData = '[mailto:<EMAIL>]';		
		$ex = '<a class="wiki"  href="mailto:<EMAIL>" rel="">mailto:<EMAIL></a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);				


		/*
		 * e-mail with description
		 */
		$inData = '[mailto:<EMAIL>|Mail to "Somebody"]';		
		$ex = '<a class="wiki"  href="mailto:<EMAIL>.xyz" rel="">Mail to "Somebody"</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);				
	}


	/**
	 * Test links to articles, blogs, ...
	 */
	function testOtherTikiPages()
	{

		/*
		 * article
		 */
		$inData = '[article1]';
		$ex = '<a class="wiki"  href="article1" rel="">article1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		

		$inData = '[article1|An Article]';
		$ex = '<a class="wiki"  href="article1" rel="">An Article</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		


		/*
		 * blog
		 */
		$inData = '[blog1]';
		$ex = '<a class="wiki"  href="blog1" rel="">blog1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		

		$inData = '[blog1|A Blog]';
		$ex = '<a class="wiki"  href="blog1" rel="">A Blog</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		


		/*
		 * forum
		 */
		$inData = '[forum1]';
		$ex = '<a class="wiki"  href="forum1" rel="">forum1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		

		$inData = '[forum1|A Forum]';
		$ex = '<a class="wiki"  href="forum1" rel="">A Forum</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			
	}



	/**
	 * Test links to web pages
	 */
	function testWebResource()
	{


		/*
		 * Web Page:
		 * - link
		 */
		$inData = '[http://www.tiki.org]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org" rel="external nofollow">http://www.tiki.org</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Web Page:
		 * - link
		 * - description
		 */
		$inData = '[http://www.tiki.org|Tiki Wiki CMS Groupware]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org" rel="external nofollow">Tiki Wiki CMS Groupware</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Web Page:
		 * - link
		 * - description
		 * - anchor
		 */
		$inData = '[http://www.tiki.org#Tiki_News_|News of the Tiki Wiki CMS Groupware]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org#Tiki_News_" rel="external nofollow">News of the Tiki Wiki CMS Groupware</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Web Page:
		 * - link
		 * - description
		 * - anchor
		 * - box
		 */
		$inData = '[http://www.tiki.org#Tiki_News_|News of the Tiki Wiki CMS Groupware|box]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org#Tiki_News_" rel="box external nofollow">News of the Tiki Wiki CMS Groupware</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * 
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|nocache]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="external nofollow">http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * - description
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|You Tube video in their flash player|nocache]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="external nofollow">You Tube video in their flash player</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * - description
		 * - box
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|You Tube video in their flash player|box]'; // additional nocache does not work		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="box external nofollow">You Tube video in their flash player</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * - description
		 * - box with dimensions
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|You Tube video in their flash player|box;width=405;height=340;]'; // additional nocache does not work		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="box;width=405;height=340; external nofollow">You Tube video in their flash player</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			
	}


	/**
	 * Test links to internal wiki pages
	 */
	function testWikiPage()
	{

		global $tikilib;


		/*
		 * 'HomePage' must exists
		 */
		$homePage = 'HomePage';
		$info = $tikilib->get_page_info($homePage, false);
		$this->assertTrue($info != null);


		/*
		 * 'Page does not exist not exist' must not exist
		 */
		$noPage = 'Page does not exist not exist';
		$info = $tikilib->get_page_info($noPage, false);
		$this->assertFalse($info);


		/*
		 * - existing page
		 */<commit_msg>removing assertions that are not related with the object being tested<commit_after><?php
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

/**
 * @group unit
 *
 */
require_once 'lib/wiki/editlib.php';
require_once 'lib/admin/adminlib.php';


class EditLib_ParseToWysiwyg_LinkTest extends TikiTestCase
{
	private $dir = '';  // the unmodifed directory
	private $el = null; // the EditLib
	private $ext1 = 'test_ext1'; // name of the external Wiki 1


	function __construct()
	{
		$this->dir = getcwd();

		// we must set the page regex, otherwise the links get not parsed
		// taken from: 'lib/setup/wiki.php' with  $prefs['wiki_page_regex'] == 'full'
		global $page_regex;
		$page_regex = '([A-Za-z0-9_]|[\x80-\xFF])([\.: A-Za-z0-9_\-]|[\x80-\xFF])*([A-Za-z0-9_]|[\x80-\xFF])';
	}


	function setUp()
	{
		$_SERVER['HTTP_HOST'] = ''; // editlib expects that HTTP_HOST is defined	
		$_SERVER['SERVER_NAME'] = 'myserver'; // the ParserLib expects the servername to be set	

		global $prefs;
		$prefs['feature_sefurl'] = 'n'; // default

		$this->el = new EditLib();

		chdir($this->dir);
		chdir('../../'); // the tiki installation directory
	}


	function tearDown()
	{
		chdir($this->dir);

		/*
		 * remove the external Wikis defined in the tests 
		 */
		global $tikilib;

		$query = 'SELECT `name`, `extwikiId` FROM `tiki_extwiki`';
		$wikis = $tikilib->fetchMap($query);
		$tmp_wikis = array($this->ext1);

		foreach ($tmp_wikis as $w) {
			if (isset($wikis[$w])) {
				$id = $wikis[$w];
				$tikilib->lib('admin')->remove_extwiki($id);
			}
		}
	}	


	/**
	 * Test links to pages of an external Wiki
	 * 
	 * This test is used to detect changes in the parser. Here, the EditLib is not used.
	 * 
	 * Note: Links with an invalid wiki identifier are parsed as regular Wiki page links.
	 */
	function testExternalWiki()
	{

		/*
		 * setup the external wikis and the parser
		 */
		global $tikilib;
		$tikilib->lib('admin')->replace_extwiki(0, 'http://tikiwiki.org/tiki-index.php?page=$page', $this->ext1);
		$p = $tikilib->lib('parser');


		/*
		 * External Wiki 
		 * - page name
		 */
		$inData = "(($this->ext1:Download))" ;		
		$ex = '<a href="http://tikiwiki.org/tiki-index.php?page=Download" class="wiki ext_page test_ext1">Download</a>';
		$out = trim($p->parse_data($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * External Wiki
		 * - page name
		 * - anchor
		 */
		$inData = "(($this->ext1:Download|#LTS_-_the_Long_Term_Support_release))" ;
		$ex = '<a href="http://tikiwiki.org/tiki-index.php?page=Download#LTS_-_the_Long_Term_Support_release" class="wiki ext_page test_ext1">Download</a>';
		$out = trim($p->parse_data($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * External Wiki
		 * - page name
		 * - anchor
		 * - description
		 */
		$inData = "(($this->ext1:Download|#LTS_-_the_Long_Term_Support_release|Download LTS))" ;	
		$ex = '<a href="http://tikiwiki.org/tiki-index.php?page=Download#LTS_-_the_Long_Term_Support_release" class="wiki ext_page test_ext1">Download LTS</a>';
		$out = trim($p->parse_data($inData));
		$this->assertEquals($ex, $out);	
	}


	/**
	 * Test link to anchor within a page
	 */
	function testInPage()
	{

		/*
		 * with description
		 */
		$inData = '[#A_Heading|Link to heading]';		
		$ex = '<a class="wiki" href="#A_Heading" rel="">Link to heading</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$out = preg_replace('/  /', ' ', $out); // the parser writes to many spaces
		$this->assertEquals($ex, $out);


		/*
		 * no description
		 */
		$inData = '[#A_Heading]';		
		$ex = '<a class="wiki" href="#A_Heading" rel="">#A_Heading</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$out = preg_replace('/  /', ' ', $out); // the parser writes to many spaces
		$this->assertEquals($ex, $out);				
	}


	/**
	 * Test link for creating e-mail
	 */
	function testMailTo()
	{

		/*
		 * e-mail
		 */
		$inData = '[mailto:<EMAIL>]';		
		$ex = '<a class="wiki"  href="mailto:<EMAIL>" rel="">mailto:<EMAIL></a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);				


		/*
		 * e-mail with description
		 */
		$inData = '[mailto:<EMAIL>|Mail to "Somebody"]';		
		$ex = '<a class="wiki"  href="mailto:<EMAIL>" rel="">Mail to "Somebody"</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);				
	}


	/**
	 * Test links to articles, blogs, ...
	 */
	function testOtherTikiPages()
	{

		/*
		 * article
		 */
		$inData = '[article1]';
		$ex = '<a class="wiki"  href="article1" rel="">article1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		

		$inData = '[article1|An Article]';
		$ex = '<a class="wiki"  href="article1" rel="">An Article</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		


		/*
		 * blog
		 */
		$inData = '[blog1]';
		$ex = '<a class="wiki"  href="blog1" rel="">blog1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		

		$inData = '[blog1|A Blog]';
		$ex = '<a class="wiki"  href="blog1" rel="">A Blog</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		


		/*
		 * forum
		 */
		$inData = '[forum1]';
		$ex = '<a class="wiki"  href="forum1" rel="">forum1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);		

		$inData = '[forum1|A Forum]';
		$ex = '<a class="wiki"  href="forum1" rel="">A Forum</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			
	}



	/**
	 * Test links to web pages
	 */
	function testWebResource()
	{


		/*
		 * Web Page:
		 * - link
		 */
		$inData = '[http://www.tiki.org]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org" rel="external nofollow">http://www.tiki.org</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Web Page:
		 * - link
		 * - description
		 */
		$inData = '[http://www.tiki.org|Tiki Wiki CMS Groupware]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org" rel="external nofollow">Tiki Wiki CMS Groupware</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Web Page:
		 * - link
		 * - description
		 * - anchor
		 */
		$inData = '[http://www.tiki.org#Tiki_News_|News of the Tiki Wiki CMS Groupware]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org#Tiki_News_" rel="external nofollow">News of the Tiki Wiki CMS Groupware</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Web Page:
		 * - link
		 * - description
		 * - anchor
		 * - box
		 */
		$inData = '[http://www.tiki.org#Tiki_News_|News of the Tiki Wiki CMS Groupware|box]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.tiki.org#Tiki_News_" rel="box external nofollow">News of the Tiki Wiki CMS Groupware</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * 
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|nocache]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="external nofollow">http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * - description
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|You Tube video in their flash player|nocache]';		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="external nofollow">You Tube video in their flash player</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * - description
		 * - box
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|You Tube video in their flash player|box]'; // additional nocache does not work		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="box external nofollow">You Tube video in their flash player</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			


		/*
		 * Link to video
		 * - link
		 * - description
		 * - box with dimensions
		 */
		$inData = '[http://www.youtube.com/v/KBewVCducWw&autoplay=1|You Tube video in their flash player|box;width=405;height=340;]'; // additional nocache does not work		
			$ex = '<a class="wiki external" target="_blank" href="http://www.youtube.com/v/KBewVCducWw&amp;autoplay=1" rel="box;width=405;height=340; external nofollow">You Tube video in their flash player</a>';
		$out = trim($this->el->parseToWysiwyg($inData));
		$this->assertEquals($ex, $out);			
	}


	/**
	 * Test links to internal wiki pages
	 */
	function testWikiPage()
	{

		global $tikilib;

		$homePage = 'HomePage';
		$noPage = 'Page does not exist not exist';
		
		/*
		 * - existing page
		 */
{'fraction_non_alphanumeric': 0.19957994224205827, 'fraction_numerical': 0.007140981885009189, 'mean_word_length': 3.4520804114071995, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 251, 'https://': 0, 'lorem ipsum': 0, 'www.': 36, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5821310', 'n_tokens_mistral': 7870, 'n_tokens_neox': 6881, 'n_words': 1437}