<commit_before><?php

/**
 * @package cms
 * @subpackage tests
 */

class CMSPageHistoryControllerTest extends FunctionalTest {
	
	static $fixture_file = 'CMSPageHistoryControllerTest.yml';
	
	private $versionUnpublishedCheck, $versionPublishCheck, $versionUnpublishedCheck2;
	private $page;
	
	function setUp() {
		parent::setUp();
		
		$this->loginWithPermission('ADMIN');
		
		// creates a series of published, unpublished versions of a page
		$this->page = new Page();
		$this->page->URLSegment = "test";
		$this->page->Content = "new content";
		$this->page->write(); 
		$this->versionUnpublishedCheck = $this->page->Version;
		
		$this->page->Content = "some further content";
		$this->page->write();
		$this->page->publish('Stage', 'Live');
		$this->versionPublishCheck = $this->page->Version;
		
		$this->page->Content = "No, more changes please";
		$this->page->Title = "Changing titles too";
		$this->page->write();
		$this->versionUnpublishedCheck2 = $this->page->Version;
		
		$this->page->Title = "Final Change";
		$this->page->write();
		$this->page->publish('Stage', 'Live');
		$this->versionPublishCheck2 = $this->page->Version;
	}
	
	function testGetEditForm() {
		$controller = new CMSPageHistoryController();
		
		// should get the latest version which we cannot rollback to
		$form = $controller->getEditForm($this->page->ID);
	
		$this->assertTrue($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());

		$this->assertEquals($this->page->ID, $form->dataFieldByName('ID')->Value());
		$this->assertEquals($this->versionPublishCheck2, $form->dataFieldByName('Version')->Value());

		$this->assertContains(
			sprintf("Currently viewing version %s.", $this->versionPublishCheck2),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
		
		// edit form with a given version
		$form = $controller->getEditForm($this->page->ID, null, $this->versionPublishCheck);
		$this->assertFalse($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());

		$this->assertEquals($this->page->ID, $form->dataFieldByName('ID')->Value());
		$this->assertEquals($this->versionPublishCheck, $form->dataFieldByName('Version')->Value());
		$this->assertContains(
			sprintf("Currently viewing version %s.", $this->versionPublishCheck),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
		
		// check that compare mode updates the message
		$form = $controller->getEditForm($this->page->ID, null, $this->versionPublishCheck, $this->versionPublishCheck2);
		$this->assertContains(
			sprintf("Comparing versions %s", $this->versionPublishCheck),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
		
		$this->assertContains(
			sprintf("and %s", $this->versionPublishCheck2),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
	}

	/**
	 * @todo should be less tied to cms theme.
	 * @todo check highlighting for comparing pages.
	 */
	function testVersionsForm() {
		$history = $this->get('admin/page/history/show/'. $this->page->ID);
		$form = $this->cssParser()->getBySelector("#Form_VersionsForm");
		
		$this->assertEquals(1, count($form));
		
		// check the page ID is present
		$hidden = $form[0]->xpath("fieldset/input[@type='hidden']");
		
		$this->assertThat($hidden, $this->logicalNot($this->isNull()), 'Hidden ID field exists');
		$this->assertEquals(4, (int) $hidden[0]->attributes()->value);
		
		// ensure that all the versions are present in the table and displayed
		$rows = $form[0]->xpath("fieldset/table/tbody/tr");
		$this->assertEquals(4, count($rows));
	}
	
	function testVersionsFormTableContainsInformation() {
		$history = $this->get('admin/page/history/show/'. $this->page->ID);
		$form = $this->cssParser()->getBySelector("#Form_VersionsForm");
		$rows = $form[0]->xpath("fieldset/table/tbody/tr");
		
		$expected = array(
			array('version' => $this->versionPublishCheck2, 'status' => 'published'),
			array('version' => $this->versionUnpublishedCheck2, 'status' => 'internal'),
			array('version' => $this->versionPublishCheck, 'status' => 'published'),
			array('version' => $this->versionUnpublishedCheck, 'status' => 'internal')<commit_msg>MINOR Fixed broken CMSPageHistoryControllerTest assuming IDs<commit_after><?php

/**
 * @package cms
 * @subpackage tests
 */

class CMSPageHistoryControllerTest extends FunctionalTest {
	
	static $fixture_file = 'CMSPageHistoryControllerTest.yml';
	
	private $versionUnpublishedCheck, $versionPublishCheck, $versionUnpublishedCheck2;
	private $page;
	
	function setUp() {
		parent::setUp();
		
		$this->loginWithPermission('ADMIN');
		
		// creates a series of published, unpublished versions of a page
		$this->page = new Page();
		$this->page->URLSegment = "test";
		$this->page->Content = "new content";
		$this->page->write(); 
		$this->versionUnpublishedCheck = $this->page->Version;
		
		$this->page->Content = "some further content";
		$this->page->write();
		$this->page->publish('Stage', 'Live');
		$this->versionPublishCheck = $this->page->Version;
		
		$this->page->Content = "No, more changes please";
		$this->page->Title = "Changing titles too";
		$this->page->write();
		$this->versionUnpublishedCheck2 = $this->page->Version;
		
		$this->page->Title = "Final Change";
		$this->page->write();
		$this->page->publish('Stage', 'Live');
		$this->versionPublishCheck2 = $this->page->Version;
	}
	
	function testGetEditForm() {
		$controller = new CMSPageHistoryController();
		
		// should get the latest version which we cannot rollback to
		$form = $controller->getEditForm($this->page->ID);
	
		$this->assertTrue($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());

		$this->assertEquals($this->page->ID, $form->dataFieldByName('ID')->Value());
		$this->assertEquals($this->versionPublishCheck2, $form->dataFieldByName('Version')->Value());

		$this->assertContains(
			sprintf("Currently viewing version %s.", $this->versionPublishCheck2),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
		
		// edit form with a given version
		$form = $controller->getEditForm($this->page->ID, null, $this->versionPublishCheck);
		$this->assertFalse($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());

		$this->assertEquals($this->page->ID, $form->dataFieldByName('ID')->Value());
		$this->assertEquals($this->versionPublishCheck, $form->dataFieldByName('Version')->Value());
		$this->assertContains(
			sprintf("Currently viewing version %s.", $this->versionPublishCheck),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
		
		// check that compare mode updates the message
		$form = $controller->getEditForm($this->page->ID, null, $this->versionPublishCheck, $this->versionPublishCheck2);
		$this->assertContains(
			sprintf("Comparing versions %s", $this->versionPublishCheck),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
		
		$this->assertContains(
			sprintf("and %s", $this->versionPublishCheck2),
			$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
		);
	}

	/**
	 * @todo should be less tied to cms theme.
	 * @todo check highlighting for comparing pages.
	 */
	function testVersionsForm() {
		$history = $this->get('admin/page/history/show/'. $this->page->ID);
		$form = $this->cssParser()->getBySelector("#Form_VersionsForm");
		
		$this->assertEquals(1, count($form));
		
		// check the page ID is present
		$hidden = $form[0]->xpath("fieldset/input[@type='hidden']");
		
		$this->assertThat($hidden, $this->logicalNot($this->isNull()), 'Hidden ID field exists');
		$this->assertEquals($this->page->ID, (int) $hidden[0]->attributes()->value);
		
		// ensure that all the versions are present in the table and displayed
		$rows = $form[0]->xpath("fieldset/table/tbody/tr");
		$this->assertEquals(4, count($rows));
	}
	
	function testVersionsFormTableContainsInformation() {
		$history = $this->get('admin/page/history/show/'. $this->page->ID);
		$form = $this->cssParser()->getBySelector("#Form_VersionsForm");
		$rows = $form[0]->xpath("fieldset/table/tbody/tr");
		
		$expected = array(
			array('version' => $this->versionPublishCheck2, 'status' => 'published'),
			array('version' => $this->versionUnpublishedCheck2, 'status' => 'internal'),
			array('version' => $this->versionPublishCheck, 'status' => 'published'),
			array('version' => $this->versionUnpublishedCheck, 'status' => 'internal')
{'fraction_non_alphanumeric': 0.1904037448800468, 'fraction_numerical': 0.003627852545348157, 'mean_word_length': 5.247076023391813, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 263, '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': '2884503', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2570, 'n_words': 464}