POST<\/dd>/');
+ $this->assertWantedText('go=[Go!]');
+ }
+
+ function testDefaultFormValues() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertField('a', '');
+ $this->assertField('b', 'Default text');
+ $this->assertField('c', '');
+ $this->assertField('d', 'd1');
+ $this->assertField('e', false);
+ $this->assertField('f', 'on');
+ $this->assertField('g', 'g3');
+ $this->assertField('h', 2);
+ $this->assertField('go', 'Go!');
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('go=[Go!]');
+ $this->assertWantedText('a=[]');
+ $this->assertWantedText('b=[Default text]');
+ $this->assertWantedText('c=[]');
+ $this->assertWantedText('d=[d1]');
+ $this->assertNoUnwantedText('e=[');
+ $this->assertWantedText('f=[on]');
+ $this->assertWantedText('g=[g3]');
+ }
+
+ function testFormSubmissionByLabel() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->setField('a', 'aaa');
+ $this->setField('b', 'bbb');
+ $this->setField('c', 'ccc');
+ $this->setField('d', 'D2');
+ $this->setField('e', 'on');
+ $this->setField('f', false);
+ $this->setField('g', 'g2');
+ $this->setField('h', 1);
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[aaa]');
+ $this->assertWantedText('b=[bbb]');
+ $this->assertWantedText('c=[ccc]');
+ $this->assertWantedText('d=[d2]');
+ $this->assertWantedText('e=[on]');
+ $this->assertNoUnwantedText('f=[');
+ $this->assertWantedText('g=[g2]');
+ }
+
+ function testAdditionalFormValues() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmit('Go!', array('add' => 'A')));
+ $this->assertWantedText('go=[Go!]');
+ $this->assertWantedText('add=[A]');
+ }
+
+ function testFormSubmissionByName() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmitByName('go'));
+ $this->assertWantedText('go=[Go!]');
+ }
+
+ function testFormSubmissionByNameAndadditionalParameters() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmitByName('go', array('add' => 'A')));
+ $this->assertWantedText('go=[Go!]');
+ $this->assertWantedText('add=[A]');
+ }
+
+ function testFormSubmissionBySubmitButtonLabeledSubmit() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmitByName('test'));
+ $this->assertWantedText('test=[Submit]');
+ }
+
+ function testFormSubmissionWithIds() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertFieldById(1, '');
+ $this->assertFieldById(2, 'Default text');
+ $this->assertFieldById(3, '');
+ $this->assertFieldById(4, 'd1');
+ $this->assertFieldById(5, false);
+ $this->setFieldById(1, 'aaa');
+ $this->setFieldById(2, 'bbb');
+ $this->setFieldById(3, 'ccc');
+ $this->setFieldById(4, 'D2');
+ $this->setFieldById(5, 'on');
+ $this->assertTrue($this->clickSubmitById(99));
+ $this->assertWantedText('a=[aaa]');
+ $this->assertWantedText('b=[bbb]');
+ $this->assertWantedText('c=[ccc]');
+ $this->assertWantedText('d=[d2]');
+ $this->assertWantedText('e=[on]');
+ $this->assertWantedText('go=[Go!]');
+ }
+
+ function testImageSubmissionByLabel() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImage('Image go!', 10, 12));
+ $this->assertWantedText('go_x=[10]');
+ $this->assertWantedText('go_y=[12]');
+ }
+
+ function testImageSubmissionByLabelWithAdditionalParameters() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImage('Image go!', 10, 12, array('add' => 'A')));
+ $this->assertWantedText('add=[A]');
+ }
+
+ function testImageSubmissionByName() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImageByName('go', 10, 12));
+ $this->assertWantedText('go_x=[10]');
+ $this->assertWantedText('go_y=[12]');
+ }
+
+ function testImageSubmissionById() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImageById(97, 10, 12));
+ $this->assertWantedText('go_x=[10]');
+ $this->assertWantedText('go_y=[12]');
+ }
+
+ function testButtonSubmissionByLabel() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmit('Button go!', 10, 12));
+ $this->assertWantedPattern('/go=\[ButtonGo\]/s');
+ }
+
+ function testSelfSubmit() {
+ $this->get('http://www.lastcraft.com/test/self_form.php');
+ $this->assertNoUnwantedPattern('/submitted<\/p>/i');
+ $this->assertNoUnwantedPattern('/
wrong form<\/p>/i');
+ $this->assertTitle('Test of form self submission');
+ $this->assertTrue($this->clickSubmit());
+ $this->assertWantedPattern('/
submitted<\/p>/i');
+ $this->assertNoUnwantedPattern('/
wrong form<\/p>/i');
+ $this->assertTitle('Test of form self submission');
+ }
+
+ function testSettingOfBlankOption() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->setField('d', ''));
+ $this->clickSubmit('Go!');
+ $this->assertWantedText('d=[]');
+ }
+ }
+
+ class TestOfLiveMultiValueWidgets extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testDefaultFormValueSubmission() {
+ $this->get('http://www.lastcraft.com/test/multiple_widget_form.html');
+ $this->assertField('a', array('a2', 'a3'));
+ $this->assertField('b', array('b2', 'b3'));
+ $this->assertField('c[]', array('c2', 'c3'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a2, a3]');
+ $this->assertWantedText('b=[b2, b3]');
+ $this->assertWantedText('c=[c2, c3]');
+ }
+
+ function testSubmittingMultipleValues() {
+ $this->get('http://www.lastcraft.com/test/multiple_widget_form.html');
+ $this->setField('a', array('a1', 'a4'));
+ $this->assertField('a', array('a1', 'a4'));
+ $this->assertField('a', array('a4', 'a1'));
+ $this->setField('b', array('b1', 'b4'));
+ $this->assertField('b', array('b1', 'b4'));
+ $this->setField('c[]', array('c1', 'c4'));
+ $this->assertField('c[]', array('c1', 'c4'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a1, a4]');
+ $this->assertWantedText('b=[b1, b4]');
+ $this->assertWantedText('c=[c1, c4]');
+ }
+
+ function testSavantStyleHiddenFieldDefaults() {
+ $this->get('http://www.lastcraft.com/test/savant_style_form.html');
+ $this->assertField('a', array('a0'));
+ $this->assertField('b', array('b0'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a0]');
+ $this->assertWantedText('b=[b0]');
+ }
+
+ function testSavantStyleHiddenDefaultsAreOverridden() {
+ $this->get('http://www.lastcraft.com/test/savant_style_form.html');
+ $this->assertTrue($this->setField('a', array('a1')));
+ $this->assertTrue($this->setField('b', 'b1'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a1]');
+ $this->assertWantedText('b=[b1]');
+ }
+
+ function testSavantStyleFormSettingById() {
+ $this->get('http://www.lastcraft.com/test/savant_style_form.html');
+ $this->assertFieldById(1, array('a0'));
+ $this->assertFieldById(4, array('b0'));
+ $this->assertTrue($this->setFieldById(2, 'a1'));
+ $this->assertTrue($this->setFieldById(5, 'b1'));
+ $this->assertTrue($this->clickSubmitById(99));
+ $this->assertWantedText('a=[a1]');
+ $this->assertWantedText('b=[b1]');
+ }
+ }
+
+ class TestOfLiveHistoryNavigation extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testRetry() {
+ $this->get('http://www.lastcraft.com/test/cookie_based_counter.php');
+ $this->assertWantedPattern('/count: 1/i');
+ $this->retry();
+ $this->assertWantedPattern('/count: 2/i');
+ $this->retry();
+ $this->assertWantedPattern('/count: 3/i');
+ }
+
+ function testOfBackButton() {
+ $this->get('http://www.lastcraft.com/test/1.html');
+ $this->clickLink('2');
+ $this->assertTitle('2');
+ $this->assertTrue($this->back());
+ $this->assertTitle('1');
+ $this->assertTrue($this->forward());
+ $this->assertTitle('2');
+ $this->assertFalse($this->forward());
+ }
+
+ function testGetRetryResubmitsData() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/network_confirm.php?a=aaa'));
+ $this->assertWantedPattern('/Request method.*?
GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testGetRetryResubmitsExtraData() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/network_confirm.php',
+ array('a' => 'aaa')));
+ $this->assertWantedPattern('/Request method.*?GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testPostRetryResubmitsData() {
+ $this->assertTrue($this->post(
+ 'http://www.lastcraft.com/test/network_confirm.php',
+ array('a' => 'aaa')));
+ $this->assertWantedPattern('/Request method.*?POST<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?POST<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testGetRetryResubmitsRepeatedData() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/network_confirm.php?a=1&a=2'));
+ $this->assertWantedPattern('/Request method.*?GET<\/dd>/');
+ $this->assertWantedText('a=[1, 2]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?GET<\/dd>/');
+ $this->assertWantedText('a=[1, 2]');
+ }
+ }
+
+ class TestOfLiveAuthentication extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testChallengeFromProtectedPage() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->assertResponse(401);
+ $this->assertAuthentication('Basic');
+ $this->assertRealm('SimpleTest basic authentication');
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->retry();
+ $this->assertResponse(200);
+ }
+
+ function testEncodedAuthenticationFetchesPage() {
+ $this->get('http://test:secret@www.lastcraft.com/test/protected/');
+ $this->assertResponse(200);
+ }
+
+ function testRealmExtendsToWholeDirectory() {
+ $this->get('http://www.lastcraft.com/test/protected/1.html');
+ $this->authenticate('test', 'secret');
+ $this->clickLink('2');
+ $this->assertResponse(200);
+ $this->clickLink('3');
+ $this->assertResponse(200);
+ }
+
+ function testRedirectKeepsAuthentication() {
+ $this->get('http://www.lastcraft.com/test/protected/local_redirect.php');
+ $this->authenticate('test', 'secret');
+ $this->assertTitle('Simple test target file');
+ }
+
+ function testSessionRestartLosesAuthentication() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->restart();
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->assertResponse(401);
+ }
+ }
+
+ class TestOfLoadingFrames extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testNoFramesContentWhenFramesDisabled() {
+ $this->ignoreFrames();
+ $this->get('http://www.lastcraft.com/test/one_page_frameset.html');
+ $this->assertTitle('Frameset for testing of SimpleTest');
+ $this->assertWantedText('This content is for no frames only');
+ }
+
+ function testPatternMatchCanReadTheOnlyFrame() {
+ $this->get('http://www.lastcraft.com/test/one_page_frameset.html');
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ $this->assertNoUnwantedText('This content is for no frames only');
+ }
+
+ function testMessyFramesetResponsesByName() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/messy_frameset.html'));
+ $this->assertTitle('Frameset for testing of SimpleTest');
+
+ $this->assertTrue($this->setFrameFocus('Front controller'));
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+
+ $this->assertTrue($this->setFrameFocus('One'));
+ $this->assertResponse(200);
+ $this->assertLink('2');
+
+ $this->assertTrue($this->setFrameFocus('Frame links'));
+ $this->assertResponse(200);
+ $this->assertLink('Set one to 2');
+
+ $this->assertTrue($this->setFrameFocus('Counter'));
+ $this->assertResponse(200);
+ $this->assertWantedText('Count: 1');
+
+ $this->assertTrue($this->setFrameFocus('Redirected'));
+ $this->assertResponse(200);
+ $this->assertWantedText('r=rrr');
+
+ $this->assertTrue($this->setFrameFocus('Protected'));
+ $this->assertResponse(401);
+
+ $this->assertTrue($this->setFrameFocus('Protected redirect'));
+ $this->assertResponse(401);
+
+ $this->assertTrue($this->setFrameFocusByIndex(1));
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+
+ $this->assertTrue($this->setFrameFocusByIndex(2));
+ $this->assertResponse(200);
+ $this->assertLink('2');
+
+ $this->assertTrue($this->setFrameFocusByIndex(3));
+ $this->assertResponse(200);
+ $this->assertLink('Set one to 2');
+
+ $this->assertTrue($this->setFrameFocusByIndex(4));
+ $this->assertResponse(200);
+ $this->assertWantedTExt('Count: 1');
+
+ $this->assertTrue($this->setFrameFocusByIndex(5));
+ $this->assertResponse(200);
+ $this->assertWantedText('r=rrr');
+
+ $this->assertTrue($this->setFrameFocusByIndex(6));
+ $this->assertResponse(401);
+
+ $this->assertTrue($this->setFrameFocusByIndex(7));
+ }
+
+ function testReloadingFramesetPage() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertWantedText('Count: 1');
+ $this->retry();
+ $this->assertWantedText('Count: 2');
+ $this->retry();
+ $this->assertWantedText('Count: 3');
+ }
+
+ function testReloadingSingleFrameWithCookieCounter() {
+ $this->get('http://www.lastcraft.com/test/counting_frameset.html');
+ $this->setFrameFocus('a');
+ $this->assertWantedText('Count: 1');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 2');
+
+ $this->setFrameFocus('a');
+ $this->retry();
+ $this->assertWantedText('Count: 3');
+ $this->retry();
+ $this->assertWantedText('Count: 4');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 2');
+ }
+
+ function testReloadingFrameWhenUnfocusedReloadsWholeFrameset() {
+ $this->get('http://www.lastcraft.com/test/counting_frameset.html');
+ $this->setFrameFocus('a');
+ $this->assertWantedText('Count: 1');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 2');
+
+ $this->clearFrameFocus('a');
+ $this->retry();
+
+ $this->assertTitle('Frameset for testing of SimpleTest');
+ $this->setFrameFocus('a');
+ $this->assertWantedText('Count: 3');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 4');
+ }
+
+ function testClickingNormalLinkReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('2');
+ $this->assertLink('3');
+ $this->assertWantedText('Simple test front controller');
+ }
+
+ function testJumpToNamedPageReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->clickLink('Index');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=index]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testJumpToUnnamedPageReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('No page');
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+ $this->assertWantedText('[action=no_page]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testJumpToUnnamedPageWithBareParameterReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Bare action');
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+ $this->assertWantedText('[action=]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testJumpToUnnamedPageWithEmptyQueryReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Empty query');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertWantedPattern('/raw get data.*?\[\].*?get data/si');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testJumpToUnnamedPageWithEmptyLinkReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Empty link');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertWantedPattern('/raw get data.*?\[\].*?get data/si');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testJumpBackADirectoryLevelReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Down one');
+ $this->assertWantedPattern('/index of \/test/i');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testSubmitToNamedPageReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->clickSubmit('Index');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Index]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitToSameDirectoryReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('Same directory');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Same+directory]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitToEmptyActionReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('Empty action');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Empty+action]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitToNoActionReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('No action');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=No+action]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitBackADirectoryLevelReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('Down one');
+ $this->assertWantedPattern('/index of \/test/i');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testTopLinkExitsFrameset() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Exit the frameset');
+ $this->assertTitle('Simple test target file');
+ }
+
+ function testLinkInOnePageCanLoadAnother() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertNoLink('3');
+ $this->clickLink('Set one to 2');
+ $this->assertLink('3');
+ $this->assertNoLink('2');
+ $this->assertTitle('Frameset for testing of SimpleTest');
+ }
+ }
+
+ class TestOfFrameAuthentication extends WebTestCase {
+
+ function testUnauthenticatedFrameSendsChallenge() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->setFrameFocus('Protected');
+ $this->assertAuthentication('Basic');
+ $this->assertRealm('SimpleTest basic authentication');
+ $this->assertResponse(401);
+ }
+
+ function testCanReadFrameFromAlreadyAuthenticatedRealm() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->authenticate('test', 'secret');
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->setFrameFocus('Protected');
+ $this->assertResponse(200);
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ }
+
+ function testCanAuthenticateFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->setFrameFocus('Protected');
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ $this->clearFrameFocus();
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testCanAuthenticateRedirectedFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->setFrameFocus('Protected redirect');
+ $this->assertResponse(401);
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ $this->clearFrameFocus();
+ $this->assertWantedText('Count: 1');
+ }
+ }
+
+ class TestOfNestedFrames extends WebTestCase {
+
+ function testCanNavigateToSpecificContent() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->assertTitle('Nested frameset for testing of SimpleTest');
+
+ $this->assertWantedPattern('/This is frame A/');
+ $this->assertWantedPattern('/This is frame B/');
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertLink('2');
+ $this->assertLink('Set one to 2');
+ $this->assertWantedPattern('/Count: 1/');
+ $this->assertWantedPattern('/r=rrr/');
+
+ $this->setFrameFocus('pair');
+ $this->assertWantedPattern('/This is frame A/');
+ $this->assertWantedPattern('/This is frame B/');
+ $this->assertNoUnwantedPattern('/Simple test front controller/');
+ $this->assertNoLink('2');
+
+ $this->setFrameFocus('aaa');
+ $this->assertWantedPattern('/This is frame A/');
+ $this->assertNoUnwantedPattern('/This is frame B/');
+
+ $this->clearFrameFocus();
+ $this->assertResponse(200);
+ $this->setFrameFocus('messy');
+ $this->assertResponse(200);
+ $this->setFrameFocus('Front controller');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertNoLink('2');
+ }
+
+ function testReloadingFramesetPage() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->assertWantedPattern('/Count: 1/');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 2/');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 3/');
+ }
+
+ function testRetryingNestedPageOnlyRetriesThatSet() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->assertWantedPattern('/Count: 1/');
+ $this->setFrameFocus('messy');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 2/');
+ $this->setFrameFocus('Counter');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 3/');
+
+ $this->clearFrameFocus();
+ $this->setFrameFocus('messy');
+ $this->setFrameFocus('Front controller');
+ $this->retry();
+
+ $this->clearFrameFocus();
+ $this->assertWantedPattern('/Count: 3/');
+ }
+
+ function testAuthenticatingNestedPage() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->setFrameFocus('messy');
+ $this->setFrameFocus('Protected');
+ $this->assertAuthentication('Basic');
+ $this->assertRealm('SimpleTest basic authentication');
+ $this->assertResponse(401);
+
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/A target for the SimpleTest test suite/');
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/adapter_test.php b/tests/UnitTests/simpletest/test/adapter_test.php
new file mode 100644
index 00000000..d7f67caf
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/adapter_test.php
@@ -0,0 +1,74 @@
+assertTrue(true, "PEAR true");
+ $this->assertFalse(false, "PEAR false");
+ }
+
+ function testName() {
+ $this->assertTrue($this->getName() == get_class($this));
+ }
+
+ function testPass() {
+ $this->pass("PEAR pass");
+ }
+
+ function testNulls() {
+ $value = null;
+ $this->assertNull($value, "PEAR null");
+ $value = 0;
+ $this->assertNotNull($value, "PEAR not null");
+ }
+
+ function testType() {
+ $this->assertType("Hello", "string", "PEAR type");
+ }
+
+ function testEquals() {
+ $this->assertEquals(12, 12, "PEAR identity");
+ $this->setLooselyTyped(true);
+ $this->assertEquals("12", 12, "PEAR equality");
+ }
+
+ function testSame() {
+ $same = &new SameTestClass();
+ $this->assertSame($same, $same, "PEAR same");
+ }
+
+ function testRegExp() {
+ $this->assertRegExp('/hello/', "A big hello from me", "PEAR regex");
+ }
+ }
+
+ class TestOfPhpUnitAdapter extends TestCase {
+ function TestOfPhpUnitAdapter() {
+ $this->TestCase("TestOfPhpUnitAdapter");
+ }
+
+ function testBoolean() {
+ $this->assert(true, "PHP Unit true");
+ }
+
+ function testName() {
+ $this->assertTrue($this->name() == "TestOfPhpUnitAdapter");
+ }
+
+ function testEquals() {
+ $this->assertEquals(12, 12, "PHP Unit equality");
+ }
+
+ function testMultilineEquals() {
+ $this->assertEquals("a\nb\n", "a\nb\n", "PHP Unit equality");
+ }
+
+ function testRegExp() {
+ $this->assertRegexp('/hello/', "A big hello from me", "PEAR regex");
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/all_tests.php b/tests/UnitTests/simpletest/test/all_tests.php
new file mode 100644
index 00000000..d5e8a9de
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/all_tests.php
@@ -0,0 +1,31 @@
+GroupTest('All tests for SimpleTest ' . SimpleTestOptions::getVersion());
+ $this->addTestCase(new UnitTests());
+ $this->addTestFile('shell_test.php');
+ $this->addTestFile('live_test.php');
+ $this->addTestFile('acceptance_test.php');
+ $this->addTestFile('real_sites_test.php');
+ }
+ }
+
+ $test = new AllTests();
+ if (SimpleReporter::inCli()) {
+ exit ($test->run(new TextReporter()) ? 0 : 1);
+ }
+ $test->run(new HtmlReporter());
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/authentication_test.php b/tests/UnitTests/simpletest/test/authentication_test.php
new file mode 100644
index 00000000..3a12a2ff
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/authentication_test.php
@@ -0,0 +1,141 @@
+assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/hello.html')));
+ }
+
+ function testInsideWithLongerUrl() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/hello.html')));
+ }
+
+ function testBelowRootIsOutside() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/more/hello.html')));
+ }
+
+ function testOldNetscapeDefinitionIsOutside() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/'));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/pathmore/hello.html')));
+ }
+
+ function testDifferentPageNameStillInside() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/hello.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/goodbye.html')));
+ }
+
+ function testNewUrlInSameDirectoryDoesNotChangeRealm() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/hello.html'));
+ $realm->stretch(new SimpleUrl('http://www.here.com/path/goodbye.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/index.html')));
+ }
+
+ function testNewUrlMakesRealmTheCommonPath() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/here/hello.html'));
+ $realm->stretch(new SimpleUrl('http://www.here.com/path/there/goodbye.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/here/index.html')));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/there/index.html')));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/paths/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/pathindex.html')));
+ }
+ }
+
+ class TestOfAuthenticator extends UnitTestCase {
+
+ function testNoRealms() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &new SimpleAuthenticator();
+ $authenticator->addHeaders($request, new SimpleUrl('http://here.com/'));
+ $request->tally();
+ }
+
+ function &createSingleRealm() {
+ $authenticator = &new SimpleAuthenticator();
+ $authenticator->addRealm(
+ new SimpleUrl('http://www.here.com/path/hello.html'),
+ 'Basic',
+ 'Sanctuary');
+ $authenticator->setIdentityForRealm('www.here.com', 'Sanctuary', 'test', 'secret');
+ return $authenticator;
+ }
+
+ function testOutsideRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://www.here.com/hello.html'));
+ $request->tally();
+ }
+
+ function testWithinRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectOnce('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://www.here.com/path/more/hello.html'));
+ $request->tally();
+ }
+
+ function testRestartingClearsRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->restartSession();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://www.here.com/hello.html'));
+ $request->tally();
+ }
+
+ function testDifferentHostIsOutsideRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://here.com/path/hello.html'));
+ $request->tally();
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/browser_test.php b/tests/UnitTests/simpletest/test/browser_test.php
new file mode 100644
index 00000000..b7d23fc4
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/browser_test.php
@@ -0,0 +1,870 @@
+assertIdentical($history->getMethod(), false);
+ $this->assertIdentical($history->getUrl(), false);
+ $this->assertIdentical($history->getParameters(), false);
+ }
+
+ function testCannotMoveInEmptyHistory() {
+ $history = &new SimpleBrowserHistory();
+ $this->assertFalse($history->back());
+ $this->assertFalse($history->forward());
+ }
+
+ function testCurrentTargetAccessors() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.here.com/'),
+ new SimpleFormEncoding());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.here.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testSecondEntryAccessors() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $this->assertIdentical($history->getMethod(), 'POST');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
+ $this->assertIdentical(
+ $history->getParameters(),
+ new SimpleFormEncoding(array('a' => 1)));
+ }
+
+ function testGoingBackwards() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $this->assertTrue($history->back());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testGoingBackwardsOffBeginning() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $this->assertFalse($history->back());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testGoingForwardsOffEnd() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $this->assertFalse($history->forward());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testGoingBackwardsAndForwards() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $this->assertTrue($history->back());
+ $this->assertTrue($history->forward());
+ $this->assertIdentical($history->getMethod(), 'POST');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
+ $this->assertIdentical(
+ $history->getParameters(),
+ new SimpleFormEncoding(array('a' => 1)));
+ }
+
+ function testNewEntryReplacesNextOne() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $history->back();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.third.com/'),
+ new SimpleFormEncoding());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.third.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testNewEntryDropsFutureEntries() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.third.com/'),
+ new SimpleFormEncoding());
+ $history->back();
+ $history->back();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.fourth.com/'),
+ new SimpleFormEncoding());
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.fourth.com/'));
+ $this->assertFalse($history->forward());
+ $history->back();
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertFalse($history->back());
+ }
+ }
+
+ class TestOfParsedPageAccess extends UnitTestCase {
+
+ function &loadPage(&$page) {
+ $response = &new MockSimpleHttpResponse($this);
+
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', $response);
+
+ $browser = &new MockParseSimpleBrowser($this);
+ $browser->setReturnReference('_createUserAgent', $agent);
+ $browser->setReturnReference('_parse', $page);
+ $browser->SimpleBrowser();
+
+ $browser->get('http://this.com/page.html');
+ return $browser;
+ }
+
+ function testAccessorsWhenNoPage() {
+ $agent = &new MockSimpleUserAgent($this);
+
+ $browser = &new MockParseSimpleBrowser($this);
+ $browser->setReturnReference('_createUserAgent', $agent);
+ $browser->SimpleBrowser();
+
+ $this->assertEqual($browser->getContent(), '');
+ }
+
+ function testParse() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getRequest', "GET here.html\r\n\r\n");
+ $page->setReturnValue('getRaw', 'Raw HTML');
+ $page->setReturnValue('getTitle', 'Here');
+ $page->setReturnValue('getFrameFocus', 'Frame');
+ $page->setReturnValue('getMimeType', 'text/html');
+ $page->setReturnValue('getResponseCode', 200);
+ $page->setReturnValue('getAuthentication', 'Basic');
+ $page->setReturnValue('getRealm', 'Somewhere');
+ $page->setReturnValue('getTransportError', 'Ouch!');
+
+ $browser = &$this->loadPage($page);
+
+ $this->assertEqual($browser->getRequest(), "GET here.html\r\n\r\n");
+ $this->assertEqual($browser->getContent(), 'Raw HTML');
+ $this->assertEqual($browser->getTitle(), 'Here');
+ $this->assertEqual($browser->getFrameFocus(), 'Frame');
+ $this->assertIdentical($browser->getResponseCode(), 200);
+ $this->assertEqual($browser->getMimeType(), 'text/html');
+ $this->assertEqual($browser->getAuthentication(), 'Basic');
+ $this->assertEqual($browser->getRealm(), 'Somewhere');
+ $this->assertEqual($browser->getTransportError(), 'Ouch!');
+ }
+
+ function testLinkAffirmationWhenPresent() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlsByLabel', array('http://www.nowhere.com'));
+ $page->expectOnce('getUrlsByLabel', array('a link label'));
+
+ $browser = &$this->loadPage($page);
+ $this->assertTrue($browser->isLink('a link label'));
+
+ $page->tally();
+ }
+
+ function testLinkAffirmationByIdWhenPresent() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlById', true, array(99));
+ $page->setReturnValue('getUrlById', false, array('*'));
+
+ $browser = &$this->loadPage($page);
+ $this->assertTrue($browser->isLinkById(99));
+ $this->assertFalse($browser->isLinkById(98));
+
+ $page->tally();
+ }
+
+ function testFormHandling() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getField', 'Value');
+ $page->expectOnce('getField', array('key'));
+ $page->expectOnce('setField', array('key', 'Value'));
+ $page->setReturnValue('getFieldById', 'Id value');
+ $page->expectOnce('getFieldById', array(99));
+ $page->expectOnce('setFieldById', array(99, 'Id value'));
+
+ $browser = &$this->loadPage($page);
+ $this->assertEqual($browser->getField('key'), 'Value');
+ $this->assertEqual($browser->getFieldById(99), 'Id value');
+ $browser->setField('key', 'Value');
+ $browser->setFieldById(99, 'Id value');
+
+ $page->tally();
+ }
+ }
+
+ class TestOfBrowserNavigation extends UnitTestCase {
+
+ function &createBrowser(&$agent, &$page) {
+ $browser = &new MockParseSimpleBrowser($this);
+ $browser->setReturnReference('_createUserAgent', $agent);
+ $browser->setReturnReference('_parse', $page);
+ $browser->SimpleBrowser();
+ return $browser;
+ }
+
+ function testClickLinkRequestsPage() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(
+ 0,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('http://this.com/page.html'), false));
+ $agent->expectArgumentsAt(
+ 1,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('http://this.com/new.html'), false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlsByLabel', array(new SimpleUrl('http://this.com/new.html')));
+ $page->expectOnce('getUrlsByLabel', array('New'));
+ $page->setReturnValue('getRaw', 'A page');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickLink('New'));
+
+ $agent->tally();
+ $page->tally();
+ }
+
+ function testClickLinkWithUnknownFrameStillRequestsWholePage() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(
+ 0,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('http://this.com/page.html'), false));
+ $target = new SimpleUrl('http://this.com/new.html');
+ $target->setTarget('missing');
+ $agent->expectArgumentsAt(
+ 1,
+ 'fetchResponse',
+ array('GET', $target, false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $parsed_url = new SimpleUrl('http://this.com/new.html');
+ $parsed_url->setTarget('missing');
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlsByLabel', array($parsed_url));
+ $page->setReturnValue('hasFrames', false);
+ $page->expectOnce('getUrlsByLabel', array('New'));
+ $page->setReturnValue('getRaw', 'A page');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickLink('New'));
+
+ $agent->tally();
+ $page->tally();
+ }
+
+ function testClickingMissingLinkFails() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlsByLabel', array());
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $this->assertTrue($browser->get('http://this.com/page.html'));
+ $this->assertFalse($browser->clickLink('New'));
+ }
+
+ function testClickIndexedLink() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(
+ 1,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('1.html'), false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue(
+ 'getUrlsByLabel',
+ array(new SimpleUrl('0.html'), new SimpleUrl('1.html')));
+ $page->setReturnValue('getRaw', 'A page');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickLink('New', 1));
+
+ $agent->tally();
+ }
+
+ function testClinkLinkById() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'GET',
+ new SimpleUrl('http://this.com/link.html'),
+ false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlById', new SimpleUrl('http://this.com/link.html'));
+ $page->expectOnce('getUrlById', array(2));
+ $page->setReturnValue('getRaw', 'A page');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickLinkById(2));
+
+ $agent->tally();
+ $page->tally();
+ }
+
+ function testClickingMissingLinkIdFails() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlById', false);
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertFalse($browser->clickLink(0));
+ }
+
+ function testSubmitFormByLabel() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'POST',
+ new SimpleUrl('http://this.com/handler.html'),
+ new SimpleFormEncoding(array('a' => 'A'))));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitButtonByLabel', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitButtonByLabel', array('Go', false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitLabel', $form);
+ $page->expectOnce('getFormBySubmitLabel', array('Go'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmit('Go'));
+
+ $agent->tally();
+ $page->tally();
+ $form->tally();
+ }
+
+ function testDefaultSubmitFormByLabel() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'GET',
+ new SimpleUrl('http://this.com/page.html'),
+ new SimpleFormEncoding(array('a' => 'A'))));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/page.html'));
+ $form->setReturnValue('getMethod', 'get');
+ $form->setReturnValue('submitButtonByLabel', new SimpleFormEncoding(array('a' => 'A')));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitLabel', $form);
+ $page->expectOnce('getFormBySubmitLabel', array('Submit'));
+ $page->setReturnValue('getRaw', 'stuff');
+ $page->setReturnValue('getUrl', new SimpleUrl('http://this.com/page.html'));
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmit());
+
+ $agent->tally();
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByName() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitButtonByName', new SimpleFormEncoding(array('a' => 'A')));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitName', $form);
+ $page->expectOnce('getFormBySubmitName', array('me'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmitByName('me'));
+
+ $page->tally();
+ }
+
+ function testSubmitFormById() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitButtonById', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitButtonById', array(99, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitId', $form);
+ $page->expectOnce('getFormBySubmitId', array(99));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmitById(99));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByImageLabel() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitImageByLabel', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitImageByLabel', array('Go!', 10, 11, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormByImageLabel', $form);
+ $page->expectOnce('getFormByImageLabel', array('Go!'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickImage('Go!', 10, 11));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByImageName() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitImageByName', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitImageByName', array('a', 10, 11, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormByImageName', $form);
+ $page->expectOnce('getFormByImageName', array('a'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickImageByName('a', 10, 11));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByImageId() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitImageById', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitImageById', array(99, 10, 11, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormByImageId', $form);
+ $page->expectOnce('getFormByImageId', array(99));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickImageById(99, 10, 11));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByFormId() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'POST',
+ new SimpleUrl('http://this.com/handler.html'),
+ new SimpleFormEncoding(array('a' => 'A'))));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submit', new SimpleFormEncoding(array('a' => 'A')));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormById', $form);
+ $page->expectOnce('getFormById', array(33));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->submitFormById(33));
+
+ $agent->tally();
+ $page->tally();
+ }
+ }
+
+ class TestOfBrowserFrames extends UnitTestCase {
+
+ function &createBrowser(&$agent) {
+ $browser = &new MockUserAgentSimpleBrowser($this);
+ $browser->setReturnReference('_createUserAgent', $agent);
+ $browser->SimpleBrowser();
+ return $browser;
+ }
+
+ function &createUserAgent($pages) {
+ $agent = &new MockSimpleUserAgent($this);
+ foreach ($pages as $url => $raw) {
+ $url = new SimpleUrl($url);
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', $url);
+ $response->setReturnValue('getContent', $raw);
+ $agent->setReturnReference('fetchResponse', $response, array('*', $url, '*'));
+ }
+ return $agent;
+ }
+
+ function testSimplePageHasNoFrames() {
+ $browser = &$this->createBrowser($this->createUserAgent(
+ array('http://site.with.no.frames/' => 'A non-framed page')));
+ $this->assertEqual(
+ $browser->get('http://site.with.no.frames/'),
+ 'A non-framed page');
+ $this->assertIdentical($browser->getFrames(), 'http://site.with.no.frames/');
+ }
+
+ function testFramesetWithNoFrames() {
+ $browser = &$this->createBrowser($this->createUserAgent(
+ array('http://site.with.no.frames/' => '')));
+ $this->assertEqual(
+ $browser->get('http://site.with.no.frames/'),
+ '');
+ $this->assertIdentical($browser->getFrames(), array());
+ }
+
+ function testFramesetWithSingleFrame() {
+ $frameset = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.one.frame/' => $frameset,
+ 'http://site.with.one.frame/frame.html' => 'A frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.one.frame/'),
+ 'A frame');
+ $this->assertIdentical(
+ $browser->getFrames(),
+ array('a' => 'http://site.with.one.frame/frame.html'));
+ }
+
+ function testTitleTakenFromFramesetPage() {
+ $frameset = 'Frameset title' .
+ '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.one.frame/' => $frameset,
+ 'http://site.with.one.frame/frame.html' => 'Page title')));
+
+ $browser->get('http://site.with.one.frame/');
+ $this->assertEqual($browser->getTitle(), 'Frameset title');
+ }
+
+ function testFramesetWithSingleUnnamedFrame() {
+ $frameset = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.one.frame/' => $frameset,
+ 'http://site.with.one.frame/frame.html' => 'One frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.one.frame/'),
+ 'One frame');
+ $this->assertIdentical(
+ $browser->getFrames(),
+ array(1 => 'http://site.with.one.frame/frame.html'));
+ }
+
+ function testFramesetWithMultipleFrames() {
+ $frameset = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.frames/'),
+ 'A frameB frameC frame');
+ $this->assertIdentical($browser->getFrames(), array(
+ 'a' => 'http://site.with.frames/frame_a.html',
+ 'b' => 'http://site.with.frames/frame_b.html',
+ 'c' => 'http://site.with.frames/frame_c.html'));
+ }
+
+ function testFrameFocusByName() {
+ $frameset = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame')));
+
+ $browser->get('http://site.with.frames/');
+ $browser->setFrameFocus('a');
+ $this->assertEqual($browser->getContent(), 'A frame');
+ $browser->setFrameFocus('b');
+ $this->assertEqual($browser->getContent(), 'B frame');
+ $browser->setFrameFocus('c');
+ $this->assertEqual($browser->getContent(), 'C frame');
+ }
+
+ function testFramesetWithSomeNamedFrames() {
+ $frameset = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame',
+ 'http://site.with.frames/frame_d.html' => 'D frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.frames/'),
+ 'A frameB frameC frameD frame');
+ $this->assertIdentical($browser->getFrames(), array(
+ 'a' => 'http://site.with.frames/frame_a.html',
+ 2 => 'http://site.with.frames/frame_b.html',
+ 'c' => 'http://site.with.frames/frame_c.html',
+ 4 => 'http://site.with.frames/frame_d.html'));
+ }
+
+ function testFrameFocusWithMixedNamesAndIndexes() {
+ $frameset = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame',
+ 'http://site.with.frames/frame_d.html' => 'D frame')));
+
+ $browser->get('http://site.with.frames/');
+ $browser->setFrameFocus('a');
+ $this->assertEqual($browser->getContent(), 'A frame');
+ $browser->setFrameFocus(2);
+ $this->assertEqual($browser->getContent(), 'B frame');
+ $browser->setFrameFocus('c');
+ $this->assertEqual($browser->getContent(), 'C frame');
+ $browser->setFrameFocus(4);
+ $this->assertEqual($browser->getContent(), 'D frame');
+ $browser->clearFrameFocus();
+ $this->assertEqual($browser->getContent(), 'A frameB frameC frameD frame');
+ }
+
+ function testNestedFrameset() {
+ $inner = '';
+ $outer = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.nested.frame/' => $outer,
+ 'http://site.with.nested.frame/inner.html' => $inner,
+ 'http://site.with.nested.frame/page.html' => 'The page')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.nested.frame/'),
+ 'The page');
+ $this->assertIdentical($browser->getFrames(), array(
+ 'inner' => array(
+ 'page' => 'http://site.with.nested.frame/page.html')));
+ }
+
+ function testCanNavigateToNestedFrame() {
+ $inner = '';
+ $outer = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.nested.frames/' => $outer,
+ 'http://site.with.nested.frames/inner.html' => $inner,
+ 'http://site.with.nested.frames/one.html' => 'Page one',
+ 'http://site.with.nested.frames/two.html' => 'Page two',
+ 'http://site.with.nested.frames/three.html' => 'Page three')));
+
+ $browser->get('http://site.with.nested.frames/');
+ $this->assertEqual($browser->getContent(), 'Page onePage twoPage three');
+
+ $this->assertTrue($browser->setFrameFocus('inner'));
+ $this->assertEqual($browser->getFrameFocus(), array('inner'));
+ $this->assertTrue($browser->setFrameFocus('one'));
+ $this->assertEqual($browser->getFrameFocus(), array('inner', 'one'));
+ $this->assertEqual($browser->getContent(), 'Page one');
+
+ $this->assertTrue($browser->setFrameFocus('two'));
+ $this->assertEqual($browser->getFrameFocus(), array('inner', 'two'));
+ $this->assertEqual($browser->getContent(), 'Page two');
+
+ $browser->clearFrameFocus();
+ $this->assertTrue($browser->setFrameFocus('three'));
+ $this->assertEqual($browser->getFrameFocus(), array('three'));
+ $this->assertEqual($browser->getContent(), 'Page three');
+
+ $this->assertTrue($browser->setFrameFocus('inner'));
+ $this->assertEqual($browser->getContent(), 'Page onePage two');
+ }
+
+ function testCanNavigateToNestedFrameByIndex() {
+ $inner = '';
+ $outer = '';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.nested.frames/' => $outer,
+ 'http://site.with.nested.frames/inner.html' => $inner,
+ 'http://site.with.nested.frames/one.html' => 'Page one',
+ 'http://site.with.nested.frames/two.html' => 'Page two',
+ 'http://site.with.nested.frames/three.html' => 'Page three')));
+
+ $browser->get('http://site.with.nested.frames/');
+ $this->assertEqual($browser->getContent(), 'Page onePage twoPage three');
+
+ $this->assertTrue($browser->setFrameFocusByIndex(1));
+ $this->assertEqual($browser->getFrameFocus(), array(1));
+ $this->assertTrue($browser->setFrameFocusByIndex(1));
+ $this->assertEqual($browser->getFrameFocus(), array(1, 1));
+ $this->assertEqual($browser->getContent(), 'Page one');
+
+ $this->assertTrue($browser->setFrameFocusByIndex(2));
+ $this->assertEqual($browser->getFrameFocus(), array(1, 2));
+ $this->assertEqual($browser->getContent(), 'Page two');
+
+ $browser->clearFrameFocus();
+ $this->assertTrue($browser->setFrameFocusByIndex(2));
+ $this->assertEqual($browser->getFrameFocus(), array(2));
+ $this->assertEqual($browser->getContent(), 'Page three');
+
+ $this->assertTrue($browser->setFrameFocusByIndex(1));
+ $this->assertEqual($browser->getContent(), 'Page onePage two');
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/dumper_test.php b/tests/UnitTests/simpletest/test/dumper_test.php
new file mode 100644
index 00000000..f0169247
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/dumper_test.php
@@ -0,0 +1,87 @@
+assertEqual(
+ $dumper->clipString("Hello", 6),
+ "Hello",
+ "Hello, 6->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello", 5),
+ "Hello",
+ "Hello, 5->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 3),
+ "Hel...",
+ "Hello world, 3->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 6, 3),
+ "Hello ...",
+ "Hello world, 6, 3->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 3, 6),
+ "...o w...",
+ "Hello world, 3, 6->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 4, 11),
+ "...orld",
+ "Hello world, 4, 11->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 4, 12),
+ "...orld",
+ "Hello world, 4, 12->%s");
+ }
+
+ function testDescribeNull() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/null/i', $dumper->describeValue(null));
+ }
+
+ function testDescribeBoolean() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/boolean/i', $dumper->describeValue(true));
+ $this->assertWantedPattern('/true/i', $dumper->describeValue(true));
+ $this->assertWantedPattern('/false/i', $dumper->describeValue(false));
+ }
+
+ function testDescribeString() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/string/i', $dumper->describeValue('Hello'));
+ $this->assertWantedPattern('/Hello/', $dumper->describeValue('Hello'));
+ }
+
+ function testDescribeInteger() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/integer/i', $dumper->describeValue(35));
+ $this->assertWantedPattern('/35/', $dumper->describeValue(35));
+ }
+
+ function testDescribeFloat() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/float/i', $dumper->describeValue(0.99));
+ $this->assertWantedPattern('/0\.99/', $dumper->describeValue(0.99));
+ }
+
+ function testDescribeArray() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/array/i', $dumper->describeValue(array(1, 4)));
+ $this->assertWantedPattern('/2/i', $dumper->describeValue(array(1, 4)));
+ }
+
+ function testDescribeObject() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern(
+ '/object/i',
+ $dumper->describeValue(new DumperDummy()));
+ $this->assertWantedPattern(
+ '/DumperDummy/i',
+ $dumper->describeValue(new DumperDummy()));
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/encoding_test.php b/tests/UnitTests/simpletest/test/encoding_test.php
new file mode 100644
index 00000000..409283d5
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/encoding_test.php
@@ -0,0 +1,134 @@
+assertIdentical($encoding->getValue('a'), false);
+ $this->assertIdentical($encoding->getKeys(), array());
+ $this->assertIdentical($encoding->asString(), '');
+ }
+
+ function testPrefilled() {
+ $encoding = &new SimpleFormEncoding(array('a' => 'aaa'));
+ $this->assertIdentical($encoding->getValue('a'), 'aaa');
+ $this->assertIdentical($encoding->getKeys(), array('a'));
+ $this->assertIdentical($encoding->asString(), 'a=aaa');
+ }
+
+ function testPrefilledWithObject() {
+ $encoding = &new SimpleFormEncoding(new SimpleFormEncoding(array('a' => 'aaa')));
+ $this->assertIdentical($encoding->getValue('a'), 'aaa');
+ $this->assertIdentical($encoding->getKeys(), array('a'));
+ $this->assertIdentical($encoding->asString(), 'a=aaa');
+ }
+
+ function testMultiplePrefilled() {
+ $encoding = &new SimpleFormEncoding(array('a' => array('a1', 'a2')));
+ $this->assertIdentical($encoding->getValue('a'), array('a1', 'a2'));
+ $this->assertIdentical($encoding->asString(), 'a=a1&a=a2');
+ }
+
+ function testSingleParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello');
+ $this->assertEqual($encoding->getValue('a'), 'Hello');
+ $this->assertIdentical($encoding->asString(), 'a=Hello');
+ }
+
+ function testFalseParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', false);
+ $this->assertEqual($encoding->getValue('a'), false);
+ $this->assertIdentical($encoding->asString(), '');
+ }
+
+ function testUrlEncoding() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello there!');
+ $this->assertIdentical($encoding->asString(), 'a=Hello+there%21');
+ }
+
+ function testMultipleParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello');
+ $encoding->add('b', 'Goodbye');
+ $this->assertIdentical($encoding->asString(), 'a=Hello&b=Goodbye');
+ }
+
+ function testEmptyParameters() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', '');
+ $encoding->add('b', '');
+ $this->assertIdentical($encoding->asString(), 'a=&b=');
+ }
+
+ function testRepeatedParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello');
+ $encoding->add('a', 'Goodbye');
+ $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye'));
+ $this->assertIdentical($encoding->asString(), 'a=Hello&a=Goodbye');
+ }
+
+ function testDefaultCoordinatesAreUnset() {
+ $encoding = &new SimpleFormEncoding();
+ $this->assertIdentical($encoding->getX(), false);
+ $this->assertIdentical($encoding->getY(), false);
+ }
+
+ function testSettingCoordinates() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->setCoordinates('32', '45');
+ $this->assertIdentical($encoding->getX(), 32);
+ $this->assertIdentical($encoding->getY(), 45);
+ $this->assertIdentical($encoding->asString(), '?32,45');
+ }
+
+ function testClearingCordinates() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->setCoordinates('32', '45');
+ $encoding->setCoordinates();
+ $this->assertIdentical($encoding->getX(), false);
+ $this->assertIdentical($encoding->getY(), false);
+ }
+
+ function testAddingLists() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', array('Hello', 'Goodbye'));
+ $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye'));
+ $this->assertIdentical($encoding->asString(), 'a=Hello&a=Goodbye');
+ }
+
+ function testMergeInHash() {
+ $encoding = &new SimpleFormEncoding(array('a' => 'A1', 'b' => 'B'));
+ $encoding->merge(array('a' => 'A2'));
+ $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
+ $this->assertIdentical($encoding->getValue('b'), 'B');
+ }
+
+ function testMergeInObject() {
+ $encoding = &new SimpleFormEncoding(array('a' => 'A1', 'b' => 'B'));
+ $encoding->merge(new SimpleFormEncoding(array('a' => 'A2')));
+ $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
+ $this->assertIdentical($encoding->getValue('b'), 'B');
+ }
+
+ function testMergeInObjectWithCordinates() {
+ $incoming = new SimpleFormEncoding(array('a' => 'A2'));
+ $incoming->setCoordinates(25, 24);
+
+ $encoding = &new SimpleFormEncoding(array('a' => 'A1'));
+ $encoding->setCoordinates(1, 2);
+ $encoding->merge($incoming);
+
+ $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
+ $this->assertIdentical($encoding->getX(), 25);
+ $this->assertIdentical($encoding->getY(), 24);
+ $this->assertIdentical($encoding->asString(), 'a=A1&a=A2?25,24');
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/errors_test.php b/tests/UnitTests/simpletest/test/errors_test.php
new file mode 100644
index 00000000..57aff612
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/errors_test.php
@@ -0,0 +1,139 @@
+clear();
+ }
+
+ function tearDown() {
+ $queue = &SimpleErrorQueue::instance();
+ $queue->clear();
+ }
+
+ function testSingleton() {
+ $this->assertReference(
+ SimpleErrorQueue::instance(),
+ SimpleErrorQueue::instance());
+ $this->assertIsA(SimpleErrorQueue::instance(), 'SimpleErrorQueue');
+ }
+
+ function testEmpty() {
+ $queue = &SimpleErrorQueue::instance();
+ $this->assertTrue($queue->isEmpty());
+ $this->assertFalse($queue->extract());
+ }
+
+ function testOrder() {
+ $queue = &SimpleErrorQueue::instance();
+ $queue->add(1024, 'Ouch', 'here.php', 100, array());
+ $this->assertFalse($queue->isEmpty());
+ $queue->add(512, 'Yuk', 'there.php', 101, array());
+ $this->assertEqual(
+ $queue->extract(),
+ array(1024, 'Ouch', 'here.php', 100, array()));
+ $this->assertEqual(
+ $queue->extract(),
+ array(512, 'Yuk', 'there.php', 101, array()));
+ $this->assertFalse($queue->extract());
+ }
+ }
+
+ class TestOfErrorTrap extends UnitTestCase {
+ var $_old;
+
+ function setUp() {
+ $this->_old = error_reporting(E_ALL);
+ set_error_handler('simpleTestErrorHandler');
+ }
+
+ function tearDown() {
+ restore_error_handler();
+ error_reporting($this->_old);
+ }
+
+ function testTrappedErrorPlacedInQueue() {
+ $queue = &SimpleErrorQueue::instance();
+ $this->assertFalse($queue->extract());
+ trigger_error('Ouch!');
+ list($severity, $message, $file, $line, $globals) = $queue->extract();
+ $this->assertEqual($message, 'Ouch!');
+ $this->assertEqual($file, __FILE__);
+ $this->assertFalse($queue->extract());
+ }
+ }
+
+ class TestOfErrors extends UnitTestCase {
+ var $_old;
+
+ function setUp() {
+ $this->_old = error_reporting(E_ALL);
+ }
+
+ function tearDown() {
+ error_reporting($this->_old);
+ }
+
+ function testDefaultWhenAllReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!');
+ $this->assertError('Ouch!');
+ }
+
+ function testNoticeWhenReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!', E_USER_NOTICE);
+ $this->assertError('Ouch!');
+ }
+
+ function testWarningWhenReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!', E_USER_WARNING);
+ $this->assertError('Ouch!');
+ }
+
+ function testErrorWhenReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!', E_USER_ERROR);
+ $this->assertError('Ouch!');
+ }
+
+ function testNoNoticeWhenNotReported() {
+ error_reporting(0);
+ trigger_error('Ouch!', E_USER_NOTICE);
+ $this->assertNoErrors();
+ }
+
+ function testNoWarningWhenNotReported() {
+ error_reporting(0);
+ trigger_error('Ouch!', E_USER_WARNING);
+ $this->assertNoErrors();
+ }
+
+ function testNoErrorWhenNotReported() {
+ error_reporting(0);
+ trigger_error('Ouch!', E_USER_ERROR);
+ $this->assertNoErrors();
+ }
+
+ function testNoticeSuppressedWhenReported() {
+ error_reporting(E_ALL);
+ @trigger_error('Ouch!', E_USER_NOTICE);
+ $this->assertNoErrors();
+ }
+
+ function testWarningSuppressedWhenReported() {
+ error_reporting(E_ALL);
+ @trigger_error('Ouch!', E_USER_WARNING);
+ $this->assertNoErrors();
+ }
+
+ function testErrorSuppressedWhenReported() {
+ error_reporting(E_ALL);
+ @trigger_error('Ouch!', E_USER_ERROR);
+ $this->assertNoErrors();
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/expectation_test.php b/tests/UnitTests/simpletest/test/expectation_test.php
new file mode 100644
index 00000000..f16db28a
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/expectation_test.php
@@ -0,0 +1,282 @@
+assertTrue($is_true->test(true));
+ $this->assertFalse($is_true->test(false));
+ $this->assertWantedPattern(
+ '/equal expectation.*?boolean: true/i',
+ $is_true->testMessage(true));
+ $this->assertWantedPattern(
+ '/fails.*?boolean.*?boolean/i',
+ $is_true->testMessage(false));
+ }
+
+ function testStringMatch() {
+ $hello = &new EqualExpectation("Hello");
+ $this->assertTrue($hello->test("Hello"));
+ $this->assertFalse($hello->test("Goodbye"));
+ $this->assertWantedPattern('/Equal expectation.*?Hello/', $hello->testMessage("Hello"));
+ $this->assertWantedPattern('/fails/', $hello->testMessage("Goodbye"));
+ $this->assertWantedPattern('/fails.*?goodbye/i', $hello->testMessage("Goodbye"));
+ }
+
+ function testStringPosition() {
+ $comparisons = array(
+ "ab" => 2,
+ "a" => 1,
+ "abcz" => 3,
+ "abz" => 2,
+ "az" => 1,
+ "z" => 0);
+ $str = &new EqualExpectation("abc");
+ foreach ($comparisons as $compare => $position) {
+ $this->assertWantedPattern(
+ "/at character $position/",
+ $str->testMessage($compare));
+ }
+ $str = &new EqualExpectation("abcd");
+ foreach ($comparisons as $compare => $position) {
+ $this->assertWantedPattern(
+ "/at character $position/",
+ $str->testMessage($compare));
+ }
+ }
+
+ function testInteger() {
+ $fifteen = &new EqualExpectation(15);
+ $this->assertTrue($fifteen->test(15));
+ $this->assertFalse($fifteen->test(14));
+ $this->assertWantedPattern(
+ '/equal expectation.*?15/i',
+ $fifteen->testMessage(15));
+ $this->assertWantedPattern(
+ '/fails.*?15.*?14/i',
+ $fifteen->testMessage(14));
+ }
+
+ function testFloat() {
+ $pi = &new EqualExpectation(3.14);
+ $this->assertTrue($pi->test(3.14));
+ $this->assertFalse($pi->test(3.15));
+ $this->assertWantedPattern(
+ '/float.*?3\.14/i',
+ $pi->testMessage(3.14));
+ $this->assertWantedPattern(
+ '/fails.*?3\.14.*?3\.15/i',
+ $pi->testMessage(3.15));
+ }
+
+ function testArray() {
+ $colours = &new EqualExpectation(array("r", "g", "b"));
+ $this->assertTrue($colours->test(array("r", "g", "b")));
+ $this->assertFalse($colours->test(array("g", "b", "r")));
+ $this->assertEqual(
+ $colours->testMessage(array("r", "g", "b")),
+ "Equal expectation [Array: 3 items]");
+ $this->assertWantedPattern('/fails/', $colours->testMessage(array("r", "g", "z")));
+ $this->assertWantedPattern(
+ '/\[2\] at character 0/',
+ $colours->testMessage(array("r", "g", "z")));
+ $this->assertWantedPattern(
+ '/key.*? does not match/',
+ $colours->testMessage(array("r", "g")));
+ $this->assertWantedPattern(
+ '/key.*? does not match/',
+ $colours->testMessage(array("r", "g", "b", "z")));
+ }
+
+ function testHash() {
+ $is_blue = &new EqualExpectation(array("r" => 0, "g" => 0, "b" => 255));
+ $this->assertTrue($is_blue->test(array("r" => 0, "g" => 0, "b" => 255)));
+ $this->assertFalse($is_blue->test(array("r" => 0, "g" => 255, "b" => 0)));
+ $this->assertWantedPattern(
+ '/array.*?3 items/i',
+ $is_blue->testMessage(array("r" => 0, "g" => 0, "b" => 255)));
+ $this->assertWantedPattern(
+ '/fails.*?\[b\]/',
+ $is_blue->testMessage(array("r" => 0, "g" => 0, "b" => 254)));
+ }
+
+ function testNestedHash() {
+ $tree = &new EqualExpectation(array(
+ "a" => 1,
+ "b" => array(
+ "c" => 2,
+ "d" => "Three")));
+ $this->assertWantedPattern(
+ '/member.*?\[b\].*?\[d\].*?at character 5/',
+ $tree->testMessage(array(
+ "a" => 1,
+ "b" => array(
+ "c" => 2,
+ "d" => "Threeish"))));
+ }
+
+ function testHashWithOutOfOrderKeysShouldStillMatch() {
+ $any_order = &new EqualExpectation(array('a' => 1, 'b' => 2));
+ $this->assertTrue($any_order->test(array('b' => 2, 'a' => 1)));
+ }
+ }
+
+ class TestOfInequality extends UnitTestCase {
+
+ function testStringMismatch() {
+ $not_hello = &new NotEqualExpectation("Hello");
+ $this->assertTrue($not_hello->test("Goodbye"));
+ $this->assertFalse($not_hello->test("Hello"));
+ $this->assertWantedPattern(
+ '/at character 0/',
+ $not_hello->testMessage("Goodbye"));
+ $this->assertWantedPattern(
+ '/matches/',
+ $not_hello->testMessage("Hello"));
+ }
+ }
+
+ class RecursiveNasty {
+ var $_me;
+
+ function RecursiveNasty() {
+ $this->_me = $this;
+ }
+ }
+
+ class TestOfIdentity extends UnitTestCase {
+
+ function testType() {
+ $string = &new IdenticalExpectation("37");
+ $this->assertTrue($string->test("37"));
+ $this->assertFalse($string->test(37));
+ $this->assertFalse($string->test("38"));
+ $this->assertWantedPattern(
+ '/identical.*?string.*?37/i',
+ $string->testMessage("37"));
+ $this->assertWantedPattern(
+ '/fails.*?37/',
+ $string->testMessage(37));
+ $this->assertWantedPattern(
+ '/at character 1/',
+ $string->testMessage("38"));
+ }
+
+ function _testNastyPhp5Bug() {
+ $this->assertFalse(new RecursiveNasty() != new RecursiveNasty());
+ }
+
+ function _testReallyHorribleRecursiveStructure() {
+ $hopeful = &new IdenticalExpectation(new RecursiveNasty());
+ $this->assertTrue($hopeful->test(new RecursiveNasty()));
+ }
+ }
+
+ class TestOfNonIdentity extends UnitTestCase {
+
+ function testType() {
+ $string = &new NotIdenticalExpectation("37");
+ $this->assertTrue($string->test("38"));
+ $this->assertTrue($string->test(37));
+ $this->assertFalse($string->test("37"));
+ $this->assertWantedPattern(
+ '/at character 1/',
+ $string->testMessage("38"));
+ $this->assertWantedPattern(
+ '/passes.*?type/',
+ $string->testMessage(37));
+ }
+ }
+
+ class TestOfPatterns extends UnitTestCase {
+
+ function testWanted() {
+ $pattern = &new WantedPatternExpectation('/hello/i');
+ $this->assertTrue($pattern->test("Hello world"));
+ $this->assertFalse($pattern->test("Goodbye world"));
+ }
+
+ function testUnwanted() {
+ $pattern = &new UnwantedPatternExpectation('/hello/i');
+ $this->assertFalse($pattern->test("Hello world"));
+ $this->assertTrue($pattern->test("Goodbye world"));
+ }
+ }
+
+ class ExpectedMethodTarget {
+ function hasThisMethod() {}
+ }
+
+ class TestOfMethodExistence extends UnitTestCase {
+
+ function testHasMethod() {
+ $instance = &new ExpectedMethodTarget();
+ $expectation = &new MethodExistsExpectation('hasThisMethod');
+ $this->assertTrue($expectation->test($instance));
+ $expectation = &new MethodExistsExpectation('doesNotHaveThisMethod');
+ $this->assertFalse($expectation->test($instance));
+ }
+ }
+
+ class TestOfIsA extends UnitTestCase {
+
+ function testString() {
+ $expectation = &new IsAExpectation('string');
+ $this->assertTrue($expectation->test('Hello'));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testBoolean() {
+ $expectation = &new IsAExpectation('boolean');
+ $this->assertTrue($expectation->test(true));
+ $this->assertFalse($expectation->test(1));
+ }
+
+ function testBool() {
+ $expectation = &new IsAExpectation('bool');
+ $this->assertTrue($expectation->test(true));
+ $this->assertFalse($expectation->test(1));
+ }
+
+ function testDouble() {
+ $expectation = &new IsAExpectation('double');
+ $this->assertTrue($expectation->test(5.0));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testFloat() {
+ $expectation = &new IsAExpectation('float');
+ $this->assertTrue($expectation->test(5.0));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testReal() {
+ $expectation = &new IsAExpectation('real');
+ $this->assertTrue($expectation->test(5.0));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testInteger() {
+ $expectation = &new IsAExpectation('integer');
+ $this->assertTrue($expectation->test(5));
+ $this->assertFalse($expectation->test(5.0));
+ }
+
+ function testInt() {
+ $expectation = &new IsAExpectation('int');
+ $this->assertTrue($expectation->test(5));
+ $this->assertFalse($expectation->test(5.0));
+ }
+ }
+
+ class TestOfNotA extends UnitTestCase {
+
+ function testString() {
+ $expectation = &new NotAExpectation('string');
+ $this->assertFalse($expectation->test('Hello'));
+ $this->assertTrue($expectation->test(5));
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/form_test.php b/tests/UnitTests/simpletest/test/form_test.php
new file mode 100644
index 00000000..b24eab2c
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/form_test.php
@@ -0,0 +1,326 @@
+ 'GET', 'action' => 'here.php', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual($form->getMethod(), 'get');
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/a/here.php'));
+ $this->assertIdentical($form->getId(), '33');
+ $this->assertNull($form->getValue('a'));
+ }
+
+ function testEmptyAction() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => '', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/a/index.html'));
+ }
+
+ function testMissingAction() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/a/index.html'));
+ }
+
+ function testRootAction() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => '/', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/'));
+ }
+
+ function testDefaultFrameTargetOnForm() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => 'here.php', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $form->setDefaultTarget('frame');
+
+ $expected = new SimpleUrl('http://host/a/here.php');
+ $expected->setTarget('frame');
+ $this->assertEqual($form->getAction(), $expected);
+ }
+
+ function testTextWidget() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleTextTag(
+ array('name' => 'me', 'type' => 'text', 'value' => 'Myself')));
+ $this->assertIdentical($form->getValue('me'), 'Myself');
+ $this->assertTrue($form->setField('me', 'Not me'));
+ $this->assertFalse($form->setField('not_present', 'Not me'));
+ $this->assertIdentical($form->getValue('me'), 'Not me');
+ $this->assertNull($form->getValue('not_present'));
+ }
+
+ function testTextWidgetById() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleTextTag(
+ array('name' => 'me', 'type' => 'text', 'value' => 'Myself', 'id' => 50)));
+ $this->assertIdentical($form->getValueById(50), 'Myself');
+ $this->assertTrue($form->setFieldById(50, 'Not me'));
+ $this->assertIdentical($form->getValueById(50), 'Not me');
+ }
+
+ function testSubmitEmpty() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $this->assertIdentical($form->submit(), new SimpleFormEncoding());
+ }
+
+ function testSubmitButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'go', 'value' => 'Go!', 'id' => '9')));
+ $this->assertTrue($form->hasSubmitName('go'));
+ $this->assertEqual($form->getValue('go'), 'Go!');
+ $this->assertEqual($form->getValueById(9), 'Go!');
+ $this->assertEqual(
+ $form->submitButtonByName('go'),
+ new SimpleFormEncoding(array('go' => 'Go!')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Go!'),
+ new SimpleFormEncoding(array('go' => 'Go!')));
+ $this->assertEqual(
+ $form->submitButtonById(9),
+ new SimpleFormEncoding(array('go' => 'Go!')));
+ }
+
+ function testSubmitWithAdditionalParameters() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'go', 'value' => 'Go!', 'id' => '9')));
+ $this->assertEqual(
+ $form->submitButtonByName('go', array('a' => 'A')),
+ new SimpleFormEncoding(array('go' => 'Go!', 'a' => 'A')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Go!', array('a' => 'A')),
+ new SimpleFormEncoding(array('go' => 'Go!', 'a' => 'A')));
+ $this->assertEqual(
+ $form->submitButtonById(9, array('a' => 'A')),
+ new SimpleFormEncoding(array('go' => 'Go!', 'a' => 'A')));
+ }
+
+ function testSubmitButtonWithLabelOfSubmit() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'test', 'value' => 'Submit', 'id' => '9')));
+ $this->assertTrue($form->hasSubmitName('test'));
+ $this->assertEqual($form->getValue('test'), 'Submit');
+ $this->assertEqual($form->getValueById(9), 'Submit');
+ $this->assertEqual(
+ $form->submitButtonByName('test'),
+ new SimpleFormEncoding(array('test' => 'Submit')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Submit'),
+ new SimpleFormEncoding(array('test' => 'Submit')));
+ $this->assertEqual(
+ $form->submitButtonById(9),
+ new SimpleFormEncoding(array('test' => 'Submit')));
+ }
+
+ function testSubmitButtonWithWhitespacePaddedLabelOfSubmit() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'test', 'value' => ' Submit ', 'id' => '9')));
+ $this->assertEqual($form->getValue('test'), ' Submit ');
+ $this->assertEqual($form->getValueById(9), ' Submit ');
+ $this->assertEqual(
+ $form->submitButtonByLabel('Submit'),
+ new SimpleFormEncoding(array('test' => ' Submit ')));
+ }
+
+ function testImageSubmitButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleImageSubmitTag(array(
+ 'type' => 'image',
+ 'src' => 'source.jpg',
+ 'name' => 'go',
+ 'alt' => 'Go!',
+ 'id' => '9')));
+ $this->assertTrue($form->hasImageLabel('Go!'));
+ $this->assertEqual(
+ $form->submitImageByLabel('Go!', 100, 101),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101)));
+ $this->assertTrue($form->hasImageName('go'));
+ $this->assertEqual(
+ $form->submitImageByName('go', 100, 101),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101)));
+ $this->assertTrue($form->hasImageId(9));
+ $this->assertEqual(
+ $form->submitImageById(9, 100, 101),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101)));
+ }
+
+ function testImageSubmitButtonWithAdditionalData() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleImageSubmitTag(array(
+ 'type' => 'image',
+ 'src' => 'source.jpg',
+ 'name' => 'go',
+ 'alt' => 'Go!',
+ 'id' => '9')));
+ $this->assertEqual(
+ $form->submitImageByLabel('Go!', 100, 101, array('a' => 'A')),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101, 'a' => 'A')));
+ $this->assertTrue($form->hasImageName('go'));
+ $this->assertEqual(
+ $form->submitImageByName('go', 100, 101, array('a' => 'A')),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101, 'a' => 'A')));
+ $this->assertTrue($form->hasImageId(9));
+ $this->assertEqual(
+ $form->submitImageById(9, 100, 101, array('a' => 'A')),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101, 'a' => 'A')));
+ }
+
+ function testButtonTag() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $widget = new SimpleButtonTag(
+ array('type' => 'submit', 'name' => 'go', 'value' => 'Go', 'id' => '9'));
+ $widget->addContent('Go!');
+ $form->addWidget($widget);
+ $this->assertTrue($form->hasSubmitName('go'));
+ $this->assertTrue($form->hasSubmitLabel('Go!'));
+ $this->assertEqual(
+ $form->submitButtonByName('go'),
+ new SimpleFormEncoding(array('go' => 'Go')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Go!'),
+ new SimpleFormEncoding(array('go' => 'Go')));
+ $this->assertEqual(
+ $form->submitButtonById(9),
+ new SimpleFormEncoding(array('go' => 'Go')));
+ }
+
+ function testSingleSelectFieldSubmitted() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $select = new SimpleSelectionTag(array('name' => 'a'));
+ $select->addTag(new SimpleOptionTag(
+ array('value' => 'aaa', 'selected' => '')));
+ $form->addWidget($select);
+ $this->assertIdentical(
+ $form->submit(),
+ new SimpleFormEncoding(array('a' => 'aaa')));
+ }
+
+ function testUnchecked() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'me', 'type' => 'checkbox')));
+ $this->assertIdentical($form->getValue('me'), false);
+ $this->assertTrue($form->setField('me', 'on'));
+ $this->assertEqual($form->getValue('me'), 'on');
+ $this->assertFalse($form->setField('me', 'other'));
+ $this->assertEqual($form->getValue('me'), 'on');
+ }
+
+ function testChecked() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'checkbox', 'checked' => '')));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ $this->assertFalse($form->setField('me', 'on'));
+ $this->assertEqual($form->getValue('me'), 'a');
+ $this->assertTrue($form->setField('me', false));
+ $this->assertEqual($form->getValue('me'), false);
+ }
+
+ function testSingleUncheckedRadioButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio')));
+ $this->assertIdentical($form->getValue('me'), false);
+ $this->assertTrue($form->setField('me', 'a'));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ }
+
+ function testSingleCheckedRadioButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio', 'checked' => '')));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ $this->assertFalse($form->setField('me', 'other'));
+ }
+
+ function testUncheckedRadioButtons() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio')));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'b', 'type' => 'radio')));
+ $this->assertIdentical($form->getValue('me'), false);
+ $this->assertTrue($form->setField('me', 'a'));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ $this->assertTrue($form->setField('me', 'b'));
+ $this->assertIdentical($form->getValue('me'), 'b');
+ $this->assertFalse($form->setField('me', 'c'));
+ $this->assertIdentical($form->getValue('me'), 'b');
+ }
+
+ function testCheckedRadioButtons() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio')));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'b', 'type' => 'radio', 'checked' => '')));
+ $this->assertIdentical($form->getValue('me'), 'b');
+ $this->assertTrue($form->setField('me', 'a'));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ }
+
+ function testMultipleFieldsWithSameKey() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'a', 'type' => 'checkbox', 'value' => 'me')));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'a', 'type' => 'checkbox', 'value' => 'you')));
+ $this->assertIdentical($form->getValue('a'), false);
+ $this->assertTrue($form->setField('a', 'me'));
+ $this->assertIdentical($form->getValue('a'), 'me');
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/frames_test.php b/tests/UnitTests/simpletest/test/frames_test.php
new file mode 100644
index 00000000..5226d070
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/frames_test.php
@@ -0,0 +1,569 @@
+setReturnValue('getTitle', 'This page');
+ $frameset = &new SimpleFrameset($page);
+ $this->assertEqual($frameset->getTitle(), 'This page');
+ }
+
+ function TestHeadersReadFromFramesetByDefault() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getHeaders', 'Header: content');
+ $page->setReturnValue('getMimeType', 'text/xml');
+ $page->setReturnValue('getResponseCode', 401);
+ $page->setReturnValue('getTransportError', 'Could not parse headers');
+ $page->setReturnValue('getAuthentication', 'Basic');
+ $page->setReturnValue('getRealm', 'Safe place');
+
+ $frameset = &new SimpleFrameset($page);
+
+ $this->assertIdentical($frameset->getHeaders(), 'Header: content');
+ $this->assertIdentical($frameset->getMimeType(), 'text/xml');
+ $this->assertIdentical($frameset->getResponseCode(), 401);
+ $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
+ $this->assertIdentical($frameset->getAuthentication(), 'Basic');
+ $this->assertIdentical($frameset->getRealm(), 'Safe place');
+ }
+
+ function testEmptyFramesetHasNoContent() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getRaw', 'This content');
+ $frameset = &new SimpleFrameset($page);
+ $this->assertEqual($frameset->getRaw(), '');
+ }
+
+ function testRawContentIsFromOnlyFrame() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getRaw', 'Stuff');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+ $this->assertEqual($frameset->getRaw(), 'Stuff');
+ }
+
+ function testRawContentIsFromAllFrames() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
+ }
+
+ function testTextContentIsFromOnlyFrame() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getText');
+
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getText', 'Stuff');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+ $this->assertEqual($frameset->getText(), 'Stuff');
+ }
+
+ function testTextContentIsFromAllFrames() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getText');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getText', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getText', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertEqual($frameset->getText(), 'Stuff1 Stuff2');
+ }
+
+ function testFieldIsFirstInFramelist() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getField', null);
+ $frame1->expectOnce('getField', array('a'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getField', 'A');
+ $frame2->expectOnce('getField', array('a'));
+
+ $frame3 = &new MockSimplePage($this);
+ $frame3->expectNever('getField');
+
+ $page = &new MockSimplePage($this);
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $frameset->addFrame($frame3);
+
+ $this->assertIdentical($frameset->getField('a'), 'A');
+ $frame1->tally();
+ $frame2->tally();
+ }
+
+ function testFrameReplacementByIndex() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->setFrame(array(1), $frame2);
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+ }
+
+ function testFrameReplacementByName() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1, 'a');
+ $frameset->setFrame(array('a'), $frame2);
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+ }
+ }
+
+ class TestOfFrameNavigation extends UnitTestCase {
+
+ function testStartsWithoutFrameFocus() {
+ $page = &new MockSimplePage($this);
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+ $this->assertFalse($frameset->getFrameFocus());
+ }
+
+ function testCanFocusOnSingleFrame() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getFrameFocus', array());
+ $frame->setReturnValue('getRaw', 'Stuff');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+
+ $this->assertFalse($frameset->setFrameFocusByIndex(0));
+ $this->assertTrue($frameset->setFrameFocusByIndex(1));
+ $this->assertEqual($frameset->getRaw(), 'Stuff');
+ $this->assertFalse($frameset->setFrameFocusByIndex(2));
+ $this->assertIdentical($frameset->getFrameFocus(), array(1));
+ }
+
+ function testContentComesFromFrameInFocus() {
+ $page = &new MockSimplePage($this);
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+ $frame1->setReturnValue('getFrameFocus', array());
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+ $frame2->setReturnValue('getFrameFocus', array());
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+
+ $this->assertTrue($frameset->setFrameFocusByIndex(1));
+ $this->assertEqual($frameset->getFrameFocus(), array(1));
+ $this->assertEqual($frameset->getRaw(), 'Stuff1');
+
+ $this->assertTrue($frameset->setFrameFocusByIndex(2));
+ $this->assertEqual($frameset->getFrameFocus(), array(2));
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+
+ $this->assertFalse($frameset->setFrameFocusByIndex(3));
+ $this->assertEqual($frameset->getFrameFocus(), array(2));
+
+ $frameset->clearFrameFocus();
+ $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
+ }
+ function testCanFocusByName() {
+ $page = &new MockSimplePage($this);
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+ $frame1->setReturnValue('getFrameFocus', array());
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+ $frame2->setReturnValue('getFrameFocus', array());
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+
+ $this->assertTrue($frameset->setFrameFocus('A'));
+ $this->assertEqual($frameset->getFrameFocus(), array('A'));
+ $this->assertEqual($frameset->getRaw(), 'Stuff1');
+
+ $this->assertTrue($frameset->setFrameFocusByIndex(2));
+ $this->assertEqual($frameset->getFrameFocus(), array('B'));
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+
+ $this->assertFalse($frameset->setFrameFocus('z'));
+
+ $frameset->clearFrameFocus();
+ $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
+ }
+ }
+
+ class TestOfFramesetPageInterface extends UnitTestCase {
+ var $_page_interface;
+ var $_frameset_interface;
+
+ function TestOfFramesetPageInterface() {
+ $this->UnitTestCase();
+ $this->_page_interface = $this->_getPageMethods();
+ $this->_frameset_interface = $this->_getFramesetMethods();
+ }
+
+ function assertListInAnyOrder($list, $expected) {
+ sort($list);
+ sort($expected);
+ $this->assertEqual($list, $expected);
+ }
+
+ function _getPageMethods() {
+ $methods = array();
+ foreach (get_class_methods('SimplePage') as $method) {
+ if (strtolower($method) == strtolower('SimplePage')) {
+ continue;
+ }
+ if (strtolower($method) == strtolower('getFrameset')) {
+ continue;
+ }
+ if (strncmp($method, '_', 1) == 0) {
+ continue;
+ }
+ if (strncmp($method, 'accept', 6) == 0) {
+ continue;
+ }
+ $methods[] = $method;
+ }
+ return $methods;
+ }
+
+ function _getFramesetMethods() {
+ $methods = array();
+ foreach (get_class_methods('SimpleFrameset') as $method) {
+ if (strtolower($method) == strtolower('SimpleFrameset')) {
+ continue;
+ }
+ if (strncmp($method, '_', 1) == 0) {
+ continue;
+ }
+ if (strncmp($method, 'add', 3) == 0) {
+ continue;
+ }
+ $methods[] = $method;
+ }
+ return $methods;
+ }
+
+ function testFramsetHasPageInterface() {
+ $difference = array();
+ foreach ($this->_page_interface as $method) {
+ if (! in_array($method, $this->_frameset_interface)) {
+ $this->fail("No [$method] in Frameset class");
+ return;
+ }
+ }
+ $this->pass('Frameset covers Page interface');
+ }
+
+ function testHeadersReadFromFrameIfInFocus() {
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getUrl', new SimpleUrl('http://localhost/stuff'));
+
+ $frame->setReturnValue('getRequest', 'POST stuff');
+ $frame->setReturnValue('getMethod', 'POST');
+ $frame->setReturnValue('getRequestData', array('a' => 'A'));
+ $frame->setReturnValue('getHeaders', 'Header: content');
+ $frame->setReturnValue('getMimeType', 'text/xml');
+ $frame->setReturnValue('getResponseCode', 401);
+ $frame->setReturnValue('getTransportError', 'Could not parse headers');
+ $frame->setReturnValue('getAuthentication', 'Basic');
+ $frame->setReturnValue('getRealm', 'Safe place');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame);
+ $frameset->setFrameFocusByIndex(1);
+
+ $url = new SimpleUrl('http://localhost/stuff');
+ $url->setTarget(1);
+ $this->assertIdentical($frameset->getUrl(), $url);
+
+ $this->assertIdentical($frameset->getRequest(), 'POST stuff');
+ $this->assertIdentical($frameset->getMethod(), 'POST');
+ $this->assertIdentical($frameset->getRequestData(), array('a' => 'A'));
+ $this->assertIdentical($frameset->getHeaders(), 'Header: content');
+ $this->assertIdentical($frameset->getMimeType(), 'text/xml');
+ $this->assertIdentical($frameset->getResponseCode(), 401);
+ $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
+ $this->assertIdentical($frameset->getAuthentication(), 'Basic');
+ $this->assertIdentical($frameset->getRealm(), 'Safe place');
+ }
+
+ function testAbsoluteUrlsComeFromBothFrames() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getAbsoluteUrls');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue(
+ 'getAbsoluteUrls',
+ array('http://www.lastcraft.com/', 'http://myserver/'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue(
+ 'getAbsoluteUrls',
+ array('http://www.lastcraft.com/', 'http://test/'));
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertListInAnyOrder(
+ $frameset->getAbsoluteUrls(),
+ array('http://www.lastcraft.com/', 'http://myserver/', 'http://test/'));
+ }
+
+ function testRelativeUrlsComeFromBothFrames() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue(
+ 'getRelativeUrls',
+ array('/', '.', '/test/', 'goodbye.php'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue(
+ 'getRelativeUrls',
+ array('/', '..', '/test/', 'hello.php'));
+
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRelativeUrls');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertListInAnyOrder(
+ $frameset->getRelativeUrls(),
+ array('/', '.', '/test/', 'goodbye.php', '..', 'hello.php'));
+ }
+
+ function testLabelledUrlsComeFromBothFrames() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue(
+ 'getUrlsByLabel',
+ array(new SimpleUrl('goodbye.php')),
+ array('a'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue(
+ 'getUrlsByLabel',
+ array(new SimpleUrl('hello.php')),
+ array('a'));
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2, 'Two');
+
+ $expected1 = new SimpleUrl('goodbye.php');
+ $expected1->setTarget(1);
+ $expected2 = new SimpleUrl('hello.php');
+ $expected2->setTarget('Two');
+ $this->assertEqual(
+ $frameset->getUrlsByLabel('a'),
+ array($expected1, $expected2));
+ }
+
+ function testUrlByIdComesFromFirstFrameToRespond() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getUrlById', new SimpleUrl('four.php'), array(4));
+ $frame1->setReturnValue('getUrlById', false, array(5));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getUrlById', false, array(4));
+ $frame2->setReturnValue('getUrlById', new SimpleUrl('five.php'), array(5));
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+
+ $four = new SimpleUrl('four.php');
+ $four->setTarget(1);
+ $this->assertEqual($frameset->getUrlById(4), $four);
+ $five = new SimpleUrl('five.php');
+ $five->setTarget(2);
+ $this->assertEqual($frameset->getUrlById(5), $five);
+ }
+
+ function testReadUrlsFromFrameInFocus() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getAbsoluteUrls', array('a'));
+ $frame1->setReturnValue('getRelativeUrls', array('r'));
+ $frame1->setReturnValue('getUrlsByLabel', array(new SimpleUrl('l')));
+ $frame1->setReturnValue('getUrlById', new SimpleUrl('i'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectNever('getAbsoluteUrls');
+ $frame2->expectNever('getRelativeUrls');
+ $frame2->expectNever('getUrlsByLabel');
+ $frame2->expectNever('getUrlById');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+ $frameset->setFrameFocus('A');
+
+ $this->assertIdentical($frameset->getAbsoluteUrls(), array('a'));
+ $this->assertIdentical($frameset->getRelativeUrls(), array('r'));
+ $expected = new SimpleUrl('l');
+ $expected->setTarget('A');
+ $this->assertIdentical($frameset->getUrlsByLabel('label'), array($expected));
+ $expected = new SimpleUrl('i');
+ $expected->setTarget('A');
+ $this->assertIdentical($frameset->getUrlById(99), $expected);
+ }
+
+ function testReadFrameTaggedUrlsFromFrameInFocus() {
+ $frame = &new MockSimplePage($this);
+
+ $by_label = new SimpleUrl('l');
+ $by_label->setTarget('L');
+ $frame->setReturnValue('getUrlsByLabel', array($by_label));
+
+ $by_id = new SimpleUrl('i');
+ $by_id->setTarget('I');
+ $frame->setReturnValue('getUrlById', $by_id);
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame, 'A');
+ $frameset->setFrameFocus('A');
+
+ $this->assertIdentical($frameset->getUrlsByLabel('label'), array($by_label));
+ $this->assertIdentical($frameset->getUrlById(99), $by_id);
+ }
+
+ function testFindingFormsByAllFinders() {
+ $finders = array(
+ 'getFormBySubmitLabel', 'getFormBySubmitName',
+ 'getFormBySubmitId', 'getFormByImageLabel',
+ 'getFormByImageName', 'getFormByImageId', 'getFormById');
+ $forms = array();
+
+ $frame = &new MockSimplePage($this);
+ for ($i = 0; $i < count($finders); $i++) {
+ $forms[$i] = &new MockSimpleForm($this);
+ $frame->setReturnReference($finders[$i], $forms[$i], array('a'));
+ }
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame(new MockSimplePage($this), 'A');
+ $frameset->addFrame($frame, 'B');
+ for ($i = 0; $i < count($finders); $i++) {
+ $method = $finders[$i];
+ $this->assertReference($frameset->$method('a'), $forms[$i]);
+ }
+
+ $frameset->setFrameFocus('A');
+ for ($i = 0; $i < count($finders); $i++) {
+ $method = $finders[$i];
+ $this->assertNull($frameset->$method('a'));
+ }
+
+ $frameset->setFrameFocus('B');
+ for ($i = 0; $i < count($finders); $i++) {
+ $method = $finders[$i];
+ $this->assertReference($frameset->$method('a'), $forms[$i]);
+ }
+ }
+
+ function testSettingAllFrameFieldsWhenNoFrameFocus() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->expectOnce('setField', array('a', 'A'));
+ $frame1->expectOnce('setFieldById', array(22, 'A'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectOnce('setField', array('a', 'A'));
+ $frame2->expectOnce('setFieldById', array(22, 'A'));
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+
+ $frameset->setField('a', 'A');
+ $frameset->setFieldById(22, 'A');
+ $frame1->tally();
+ $frame2->tally();
+ }
+
+ function testOnlySettingFieldFromFocusedFrame() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->expectOnce('setField', array('a', 'A'));
+ $frame1->expectOnce('setFieldById', array(22, 'A'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectNever('setField');
+ $frame2->expectNever('setFieldById');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+ $frameset->setFrameFocus('A');
+
+ $frameset->setField('a', 'A');
+ $frameset->setFieldById(22, 'A');
+ $frame1->tally();
+ }
+
+ function testOnlyGettingFieldFromFocusedFrame() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getField', 'f', array('a'));
+ $frame1->setReturnValue('getFieldById', 'i', array(7));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectNever('getField');
+ $frame2->expectNever('getFieldById');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+ $frameset->setFrameFocus('A');
+
+ $this->assertIdentical($frameset->getField('a'), 'f');
+ $this->assertIdentical($frameset->getFieldById(7), 'i');
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/http_test.php b/tests/UnitTests/simpletest/test/http_test.php
new file mode 100644
index 00000000..ca201be0
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/http_test.php
@@ -0,0 +1,567 @@
+assertFalse($cookie->getValue());
+ $this->assertEqual($cookie->getPath(), "/");
+ $this->assertIdentical($cookie->getHost(), false);
+ $this->assertFalse($cookie->getExpiry());
+ $this->assertFalse($cookie->isSecure());
+ }
+
+ function testCookieAccessors() {
+ $cookie = new SimpleCookie(
+ "name",
+ "value",
+ "/path",
+ "Mon, 18 Nov 2002 15:50:29 GMT",
+ true);
+ $this->assertEqual($cookie->getName(), "name");
+ $this->assertEqual($cookie->getValue(), "value");
+ $this->assertEqual($cookie->getPath(), "/path/");
+ $this->assertEqual($cookie->getExpiry(), "Mon, 18 Nov 2002 15:50:29 GMT");
+ $this->assertTrue($cookie->isSecure());
+ }
+
+ function testFullHostname() {
+ $cookie = new SimpleCookie("name");
+ $this->assertTrue($cookie->setHost("host.name.here"));
+ $this->assertEqual($cookie->getHost(), "host.name.here");
+ $this->assertTrue($cookie->setHost("host.com"));
+ $this->assertEqual($cookie->getHost(), "host.com");
+ }
+
+ function testHostTruncation() {
+ $cookie = new SimpleCookie("name");
+ $cookie->setHost("this.host.name.here");
+ $this->assertEqual($cookie->getHost(), "host.name.here");
+ $cookie->setHost("this.host.com");
+ $this->assertEqual($cookie->getHost(), "host.com");
+ $this->assertTrue($cookie->setHost("dashes.in-host.com"));
+ $this->assertEqual($cookie->getHost(), "in-host.com");
+ }
+
+ function testBadHosts() {
+ $cookie = new SimpleCookie("name");
+ $this->assertFalse($cookie->setHost("gibberish"));
+ $this->assertFalse($cookie->setHost("host.here"));
+ $this->assertFalse($cookie->setHost("host..com"));
+ $this->assertFalse($cookie->setHost("..."));
+ $this->assertFalse($cookie->setHost("host.com."));
+ }
+
+ function testHostValidity() {
+ $cookie = new SimpleCookie("name");
+ $cookie->setHost("this.host.name.here");
+ $this->assertTrue($cookie->isValidHost("host.name.here"));
+ $this->assertTrue($cookie->isValidHost("that.host.name.here"));
+ $this->assertFalse($cookie->isValidHost("bad.host"));
+ $this->assertFalse($cookie->isValidHost("nearly.name.here"));
+ }
+
+ function testPathValidity() {
+ $cookie = new SimpleCookie("name", "value", "/path");
+ $this->assertFalse($cookie->isValidPath("/"));
+ $this->assertTrue($cookie->isValidPath("/path/"));
+ $this->assertTrue($cookie->isValidPath("/path/more"));
+ }
+
+ function testSessionExpiring() {
+ $cookie = new SimpleCookie("name", "value", "/path");
+ $this->assertTrue($cookie->isExpired(0));
+ }
+
+ function testTimestampExpiry() {
+ $cookie = new SimpleCookie("name", "value", "/path", 456);
+ $this->assertFalse($cookie->isExpired(0));
+ $this->assertTrue($cookie->isExpired(457));
+ $this->assertFalse($cookie->isExpired(455));
+ }
+
+ function testDateExpiry() {
+ $cookie = new SimpleCookie(
+ "name",
+ "value",
+ "/path",
+ "Mon, 18 Nov 2002 15:50:29 GMT");
+ $this->assertTrue($cookie->isExpired("Mon, 18 Nov 2002 15:50:30 GMT"));
+ $this->assertFalse($cookie->isExpired("Mon, 18 Nov 2002 15:50:28 GMT"));
+ }
+
+ function testAging() {
+ $cookie = new SimpleCookie("name", "value", "/path", 200);
+ $cookie->agePrematurely(199);
+ $this->assertFalse($cookie->isExpired(0));
+ $cookie->agePrematurely(2);
+ $this->assertTrue($cookie->isExpired(0));
+ }
+ }
+
+ class TestOfDirectRoute extends UnitTestCase {
+
+ function testDefaultGetRequest() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET /here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html'));
+
+ $this->assertReference($route->createConnection('GET', 15), $socket);
+ $socket->tally();
+ }
+
+ function testDefaultPostRequest() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("POST /here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html'));
+
+ $route->createConnection('POST', 15);
+ $socket->tally();
+ }
+
+ function testGetWithPort() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET /here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host:81\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host:81/here.html'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testGetWithParameters() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET /here.html?a=1&b=2 HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html?a=1&b=2'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+ }
+
+ class TestOfProxyRoute extends UnitTestCase {
+
+ function testDefaultGet() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html'),
+ new SimpleUrl('http://my-proxy'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testDefaultPost() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("POST http://a.valid.host/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html'),
+ new SimpleUrl('http://my-proxy'));
+
+ $route->createConnection('POST', 15);
+ $socket->tally();
+ }
+
+ function testGetWithPort() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host:81/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8081\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host:81/here.html'),
+ new SimpleUrl('http://my-proxy:8081'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testGetWithParameters() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html?a=1&b=2 HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html?a=1&b=2'),
+ new SimpleUrl('http://my-proxy'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testGetWithAuthentication() {
+ $encoded = base64_encode('Me:Secret');
+
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Proxy-Authorization: Basic $encoded\r\n"));
+ $socket->expectArgumentsAt(3, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 4);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html'),
+ new SimpleUrl('http://my-proxy'),
+ 'Me',
+ 'Secret');
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+ }
+
+ class TestOfHttpRequest extends UnitTestCase {
+
+ function testReadingBadConnection() {
+ $socket = &new MockSimpleSocket($this);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+
+ $reponse = &$request->fetch(15);
+ $this->assertTrue($reponse->isError());
+ }
+
+ function testReadingGoodConnection() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectOnce('write', array("\r\n"));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+ $route->expectArguments('createConnection', array('GET', 15));
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ $route->tally();
+ }
+
+ function testWritingAdditionalHeaders() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("My: stuff\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("\r\n"));
+ $socket->expectCallCount('write', 2);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+ $request->addHeaderLine('My: stuff');
+ $request->fetch(15);
+
+ $socket->tally();
+ }
+
+ function testCookieWriting() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Cookie: a=A\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("\r\n"));
+ $socket->expectCallCount('write', 2);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+ $request->setCookie(new SimpleCookie('a', 'A'));
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ }
+
+ function testMultipleCookieWriting() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Cookie: a=A;b=B\r\n"));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+ $request->setCookie(new SimpleCookie('a', 'A'));
+ $request->setCookie(new SimpleCookie('b', 'B'));
+
+ $request->fetch(15);
+ $socket->tally();
+ }
+ }
+
+ class TestOfHttpPostRequest extends UnitTestCase {
+
+ function testReadingBadConnection() {
+ $socket = &new MockSimpleSocket($this);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'POST', '');
+
+ $reponse = &$request->fetch(15);
+ $this->assertTrue($reponse->isError());
+ }
+
+ function testReadingGoodConnection() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Content-Length: 0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("\r\n"));
+ $socket->expectArgumentsAt(3, 'write', array(""));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+ $route->expectArguments('createConnection', array('POST', 15));
+
+ $request = &new SimpleHttpRequest($route, 'POST', new SimpleFormEncoding());
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ $route->tally();
+ }
+
+ function testContentHeadersCalculated() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Content-Length: 3\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("\r\n"));
+ $socket->expectArgumentsAt(3, 'write', array("a=A"));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+ $route->expectArguments('createConnection', array('POST', 15));
+
+ $request = &new SimpleHttpRequest(
+ $route,
+ 'POST',
+ new SimpleFormEncoding(array('a' => 'A')));
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ $route->tally();
+ }
+ }
+
+ class TestOfHttpHeaders extends UnitTestCase {
+
+ function testParseBasicHeaders() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 200 OK\r\n" .
+ "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n" .
+ "Connection: close");
+ $this->assertIdentical($headers->getHttpVersion(), "1.1");
+ $this->assertIdentical($headers->getResponseCode(), 200);
+ $this->assertEqual($headers->getMimeType(), "text/plain");
+ }
+
+ function testParseOfCookies() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 200 OK\r\n" .
+ "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n" .
+ "Set-Cookie: a=aaa; expires=Wed, 25-Dec-02 04:24:20 GMT; path=/here/\r\n" .
+ "Set-Cookie: b=bbb\r\n" .
+ "Connection: close");
+ $cookies = $headers->getNewCookies();
+ $this->assertEqual(count($cookies), 2);
+ $this->assertEqual($cookies[0]->getName(), "a");
+ $this->assertEqual($cookies[0]->getValue(), "aaa");
+ $this->assertEqual($cookies[0]->getPath(), "/here/");
+ $this->assertEqual($cookies[0]->getExpiry(), "Wed, 25 Dec 2002 04:24:20 GMT");
+ $this->assertEqual($cookies[1]->getName(), "b");
+ $this->assertEqual($cookies[1]->getValue(), "bbb");
+ $this->assertEqual($cookies[1]->getPath(), "/");
+ $this->assertEqual($cookies[1]->getExpiry(), "");
+ }
+
+ function testRedirect() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 301 OK\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Content-Length: 0\r\n" .
+ "Location: http://www.somewhere-else.com/\r\n" .
+ "Connection: close");
+ $this->assertIdentical($headers->getResponseCode(), 301);
+ $this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com/");
+ $this->assertTrue($headers->isRedirect());
+ }
+
+ function testParseChallenge() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 401 Authorization required\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Connection: close\r\n" .
+ "WWW-Authenticate: Basic realm=\"Somewhere\"");
+ $this->assertEqual($headers->getAuthentication(), 'Basic');
+ $this->assertEqual($headers->getRealm(), 'Somewhere');
+ $this->assertTrue($headers->isChallenge());
+ }
+ }
+
+ class TestOfHttpResponse extends UnitTestCase {
+
+ function testBadRequest() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValue('getSent', '');
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertTrue($response->isError());
+ $this->assertWantedPattern('/Nothing fetched/', $response->getError());
+ $this->assertIdentical($response->getContent(), false);
+ $this->assertIdentical($response->getSent(), '');
+ }
+
+ function testBadSocketDuringResponse() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValue("read", "");
+ $socket->setReturnValue('getSent', 'HTTP/1.1 ...');
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertTrue($response->isError());
+ $this->assertEqual($response->getContent(), '');
+ $this->assertEqual($response->getSent(), 'HTTP/1.1 ...');
+ }
+
+ function testIncompleteHeader() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValueAt(2, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertTrue($response->isError());
+ $this->assertEqual($response->getContent(), "");
+ }
+
+ function testParseOfResponseHeaders() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\nDate: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(2, "read", "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\nConne");
+ $socket->setReturnValueAt(3, "read", "ction: close\r\n\r\nthis is a test file\n");
+ $socket->setReturnValueAt(4, "read", "with two lines in it\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertFalse($response->isError());
+ $this->assertEqual(
+ $response->getContent(),
+ "this is a test file\nwith two lines in it\n");
+ $headers = $response->getHeaders();
+ $this->assertIdentical($headers->getHttpVersion(), "1.1");
+ $this->assertIdentical($headers->getResponseCode(), 200);
+ $this->assertEqual($headers->getMimeType(), "text/plain");
+ $this->assertFalse($headers->isRedirect());
+ $this->assertFalse($headers->getLocation());
+ }
+
+ function testParseOfCookies() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValueAt(2, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(3, "read", "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n");
+ $socket->setReturnValueAt(4, "read", "Set-Cookie: a=aaa; expires=Wed, 25-Dec-02 04:24:20 GMT; path=/here/\r\n");
+ $socket->setReturnValueAt(5, "read", "Connection: close\r\n");
+ $socket->setReturnValueAt(6, "read", "\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertFalse($response->isError());
+ $headers = $response->getHeaders();
+ $cookies = $headers->getNewCookies();
+ $this->assertEqual($cookies[0]->getName(), "a");
+ $this->assertEqual($cookies[0]->getValue(), "aaa");
+ $this->assertEqual($cookies[0]->getPath(), "/here/");
+ $this->assertEqual($cookies[0]->getExpiry(), "Wed, 25 Dec 2002 04:24:20 GMT");
+ }
+
+ function testRedirect() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 301 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(2, "read", "Location: http://www.somewhere-else.com/\r\n");
+ $socket->setReturnValueAt(3, "read", "Connection: close\r\n");
+ $socket->setReturnValueAt(4, "read", "\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $headers = $response->getHeaders();
+ $this->assertTrue($headers->isRedirect());
+ $this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com/");
+ }
+
+ function testRedirectWithPort() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 301 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(2, "read", "Location: http://www.somewhere-else.com:80/\r\n");
+ $socket->setReturnValueAt(3, "read", "Connection: close\r\n");
+ $socket->setReturnValueAt(4, "read", "\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $headers = $response->getHeaders();
+ $this->assertTrue($headers->isRedirect());
+ $this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com:80/");
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/live_test.php b/tests/UnitTests/simpletest/test/live_test.php
new file mode 100644
index 00000000..193284e7
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/live_test.php
@@ -0,0 +1,47 @@
+assertTrue($socket->isError());
+ $this->assertWantedPattern(
+ '/Cannot open \\[bad_url:111\\] with \\[.*?\\] within \\[5\\] seconds/',
+ $socket->getError());
+ $this->assertFalse($socket->isOpen());
+ $this->assertFalse($socket->write('A message'));
+ }
+
+ function testSocketClosure() {
+ $socket = &new SimpleSocket('www.lastcraft.com', 80, 15);
+ $this->assertTrue($socket->isOpen());
+ $this->assertTrue($socket->write("GET /test/network_confirm.php HTTP/1.0\r\n"));
+ $socket->write("Host: www.lastcraft.com\r\n");
+ $socket->write("Connection: close\r\n\r\n");
+ $this->assertEqual($socket->read(8), "HTTP/1.1");
+ $socket->close();
+ $this->assertIdentical($socket->read(8), false);
+ }
+
+ function testRecordOfSentCharacters() {
+ $socket = &new SimpleSocket('www.lastcraft.com', 80, 15);
+ $this->assertTrue($socket->write("GET /test/network_confirm.php HTTP/1.0\r\n"));
+ $socket->write("Host: www.lastcraft.com\r\n");
+ $socket->write("Connection: close\r\n\r\n");
+ $socket->close();
+ $this->assertEqual($socket->getSent(),
+ "GET /test/network_confirm.php HTTP/1.0\r\n" .
+ "Host: www.lastcraft.com\r\n" .
+ "Connection: close\r\n\r\n");
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/options_test.php b/tests/UnitTests/simpletest/test/options_test.php
new file mode 100644
index 00000000..8535f739
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/options_test.php
@@ -0,0 +1,95 @@
+assertEqual(SimpleTestOptions::getMockBaseClass(), 'Fred');
+ SimpleTestOptions::setMockBaseClass($old_class);
+ }
+
+ function testStubBase() {
+ $old_class = SimpleTestOptions::getStubBaseClass();
+ SimpleTestOptions::setStubBaseClass('Fred');
+ $this->assertEqual(SimpleTestOptions::getStubBaseClass(), 'Fred');
+ SimpleTestOptions::setStubBaseClass($old_class);
+ }
+
+ function testIgnoreList() {
+ $this->assertFalse(SimpleTestOptions::isIgnored('ImaginaryTestCase'));
+ SimpleTestOptions::ignore('ImaginaryTestCase');
+ $this->assertTrue(SimpleTestOptions::isIgnored('ImaginaryTestCase'));
+ }
+ }
+
+ class ComparisonClass {
+ }
+
+ class ComparisonSubclass extends ComparisonClass {
+ }
+
+ class TestOfCompatibility extends UnitTestCase {
+
+ function testIsA() {
+ $this->assertTrue(SimpleTestCompatibility::isA(
+ new ComparisonClass(),
+ 'ComparisonClass'));
+ $this->assertFalse(SimpleTestCompatibility::isA(
+ new ComparisonClass(),
+ 'ComparisonSubclass'));
+ $this->assertTrue(SimpleTestCompatibility::isA(
+ new ComparisonSubclass(),
+ 'ComparisonClass'));
+ }
+
+ function testIdentityOfObjects() {
+ $object1 = new ComparisonClass();
+ $object2 = new ComparisonClass();
+ $this->assertIdentical($object1, $object2);
+ }
+
+ function testReferences () {
+ $thing = "Hello";
+ $thing_reference = &$thing;
+ $thing_copy = $thing;
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $thing,
+ $thing));
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $thing,
+ $thing_reference));
+ $this->assertFalse(SimpleTestCompatibility::isReference(
+ $thing,
+ $thing_copy));
+ }
+
+ function testObjectReferences () {
+ $object = &new ComparisonClass();
+ $object_reference = &$object;
+ $object_copy = new ComparisonClass();
+ $object_assignment = $object;
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $object,
+ $object));
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $object,
+ $object_reference));
+ $this->assertFalse(SimpleTestCompatibility::isReference(
+ $object,
+ $object_copy));
+ if (version_compare(phpversion(), '5', '>=')) {
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $object,
+ $object_assignment));
+ } else {
+ $this->assertFalse(SimpleTestCompatibility::isReference(
+ $object,
+ $object_assignment));
+ }
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/page_test.php b/tests/UnitTests/simpletest/test/page_test.php
new file mode 100644
index 00000000..d9e87d12
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/page_test.php
@@ -0,0 +1,792 @@
+ 'http://somewhere'));
+ $tag->addContent('Label');
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments('acceptTag', array($tag));
+ $page->expectCallCount('acceptTag', 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $this->assertTrue($builder->startElement(
+ 'a',
+ array('href' => 'http://somewhere')));
+ $this->assertTrue($builder->addContent('Label'));
+ $this->assertTrue($builder->endElement('a'));
+
+ $page->tally();
+ }
+
+ function testLinkWithId() {
+ $tag = &new SimpleAnchorTag(array("href" => "http://somewhere", "id" => "44"));
+ $tag->addContent("Label");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments("acceptTag", array($tag));
+ $page->expectCallCount("acceptTag", 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $this->assertTrue($builder->startElement(
+ "a",
+ array("href" => "http://somewhere", "id" => "44")));
+ $this->assertTrue($builder->addContent("Label"));
+ $this->assertTrue($builder->endElement("a"));
+
+ $page->tally();
+ }
+
+ function testLinkExtraction() {
+ $tag = &new SimpleAnchorTag(array("href" => "http://somewhere"));
+ $tag->addContent("Label");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments("acceptTag", array($tag));
+ $page->expectCallCount("acceptTag", 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $this->assertTrue($builder->addContent("Starting stuff"));
+ $this->assertTrue($builder->startElement(
+ "a",
+ array("href" => "http://somewhere")));
+ $this->assertTrue($builder->addContent("Label"));
+ $this->assertTrue($builder->endElement("a"));
+ $this->assertTrue($builder->addContent("Trailing stuff"));
+
+ $page->tally();
+ }
+
+ function testMultipleLinks() {
+ $a1 = new SimpleAnchorTag(array("href" => "http://somewhere"));
+ $a1->addContent("1");
+
+ $a2 = new SimpleAnchorTag(array("href" => "http://elsewhere"));
+ $a2->addContent("2");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArgumentsAt(0, "acceptTag", array($a1));
+ $page->expectArgumentsAt(1, "acceptTag", array($a2));
+ $page->expectCallCount("acceptTag", 2);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $builder->startElement("a", array("href" => "http://somewhere"));
+ $builder->addContent("1");
+ $builder->endElement("a");
+ $builder->addContent("Padding");
+ $builder->startElement("a", array("href" => "http://elsewhere"));
+ $builder->addContent("2");
+ $builder->endElement("a");
+
+ $page->tally();
+ }
+
+ function testTitle() {
+ $tag = &new SimpleTitleTag(array());
+ $tag->addContent("HereThere");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments("acceptTag", array($tag));
+ $page->expectCallCount("acceptTag", 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $builder->startElement("title", array());
+ $builder->addContent("Here");
+ $builder->addContent("There");
+ $builder->endElement("title");
+
+ $page->tally();
+ }
+
+ function testForm() {
+ $page = &new MockSimplePage($this);
+ $page->expectOnce("acceptFormStart", array(new SimpleFormTag(array())));
+ $page->expectOnce("acceptFormEnd", array());
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $builder->startElement("form", array());
+ $builder->addContent("Stuff");
+ $builder->endElement("form");
+ $page->tally();
+ }
+ }
+
+ class TestOfPageParsing extends UnitTestCase {
+
+ function testParseMechanics() {
+ $parser = &new MockSimpleSaxParser($this);
+ $parser->expectOnce('parse', array('stuff'));
+
+ $page = &new MockSimplePage($this);
+ $page->expectOnce('acceptPageEnd');
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', $parser);
+ $builder->SimplePageBuilder();
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'stuff');
+
+ $builder->parse($response);
+ $parser->tally();
+ $page->tally();
+ }
+ }
+
+ class TestOfErrorPage extends UnitTestCase {
+
+ function testInterface() {
+ $page = &new SimplePage();
+ $this->assertEqual($page->getTransportError(), 'No page fetched yet');
+ $this->assertIdentical($page->getRaw(), false);
+ $this->assertIdentical($page->getHeaders(), false);
+ $this->assertIdentical($page->getMimeType(), false);
+ $this->assertIdentical($page->getResponseCode(), false);
+ $this->assertIdentical($page->getAuthentication(), false);
+ $this->assertIdentical($page->getRealm(), false);
+ $this->assertFalse($page->hasFrames());
+ $this->assertIdentical($page->getAbsoluteUrls(), array());
+ $this->assertIdentical($page->getRelativeUrls(), array());
+ $this->assertIdentical($page->getTitle(), false);
+ }
+ }
+
+ class TestOfPageHeaders extends UnitTestCase {
+
+ function testUrlAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+ $response->setReturnValue('getMethod', 'POST');
+ $response->setReturnValue('getUrl', new SimpleUrl('here'));
+ $response->setReturnValue('getRequestData', array('a' => 'A'));
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getMethod(), 'POST');
+ $this->assertEqual($page->getUrl(), new SimpleUrl('here'));
+ $this->assertEqual($page->getRequestData(), array('a' => 'A'));
+ }
+
+ function testTransportError() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getError', 'Ouch');
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getTransportError(), 'Ouch');
+ }
+
+ function testHeadersAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getRaw', 'My: Headers');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getHeaders(), 'My: Headers');
+ }
+
+ function testMimeAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getMimeType', 'text/html');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getMimeType(), 'text/html');
+ }
+
+ function testResponseAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getResponseCode', 301);
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertIdentical($page->getResponseCode(), 301);
+ }
+
+ function testAuthenticationAccessors() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getAuthentication', 'Basic');
+ $headers->setReturnValue('getRealm', 'Secret stuff');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getAuthentication(), 'Basic');
+ $this->assertEqual($page->getRealm(), 'Secret stuff');
+ }
+ }
+
+ class TestOfHtmlPage extends UnitTestCase {
+
+ function testRawAccessor() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'Raw HTML');
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getRaw(), 'Raw HTML');
+ }
+
+ function testTextAccessor() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'Some "messy" HTML');
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getText(), 'Some "messy" HTML');
+ }
+
+ function testNoLinks() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $this->assertIdentical($page->getAbsoluteUrls(), array(), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array(), 'rel->%s');
+ $this->assertIdentical($page->getUrlsByLabel('Label'), array());
+ }
+
+ function testAddAbsoluteLink() {
+ $link = &new SimpleAnchorTag(array('href' => 'http://somewhere.com'));
+ $link->addContent('Label');
+
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->AcceptTag($link);
+
+ $this->assertEqual($page->getAbsoluteUrls(), array('http://somewhere.com'), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array(), 'rel->%s');
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://somewhere.com')));
+ }
+
+ function testAddStrictRelativeLink() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php'));
+ $link->addContent('Label');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual($page->getAbsoluteUrls(), array(), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array('./somewhere.php'), 'rel->%s');
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testAddRelativeLink() {
+ $link = &new SimpleAnchorTag(array('href' => 'somewhere.php'));
+ $link->addContent('Label');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual($page->getAbsoluteUrls(), array(), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array('somewhere.php'), 'rel->%s');
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testLinkIds() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
+ $link->addContent('Label');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ $this->assertFalse($page->getUrlById(0));
+ $this->assertEqual(
+ $page->getUrlById(33),
+ new SimpleUrl('http://host/somewhere.php'));
+ }
+
+ function testFindLinkWithNormalisation() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
+ $link->addContent(' Long & thin ');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual(
+ $page->getUrlsByLabel('Long & thin'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testFindLinkWithImage() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
+ $link->addContent('
');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual(
+ $page->getUrlsByLabel(''),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testTitleSetting() {
+ $title = &new SimpleTitleTag(array());
+ $title->addContent('Title');
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->AcceptTag($title);
+ $this->assertEqual($page->getTitle(), 'Title');
+ }
+
+ function testFramesetAbsence() {
+ $url = new SimpleUrl('here');
+ $response = new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', $url);
+ $page = &new SimplePage($response);
+ $this->assertFalse($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), false);
+ }
+
+ function testHasEmptyFrameset() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->acceptFramesetStart(new SimpleTag('frameset', array()));
+ $page->acceptFramesetEnd();
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array());
+ }
+
+ function testFramesInPage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://here'));
+
+ $page = &new SimplePage($response);
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '1.html')));
+ $page->acceptFramesetStart(new SimpleTag('frameset', array()));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '2.html')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '3.html')));
+ $page->acceptFramesetEnd();
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '4.html')));
+
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array(
+ 1 => new SimpleUrl('http://here/2.html'),
+ 2 => new SimpleUrl('http://here/3.html')));
+ }
+
+ function testNamedFramesInPage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://here'));
+
+ $page = &new SimplePage($response);
+ $page->acceptFramesetStart(new SimpleTag('frameset', array()));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '1.html')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '2.html', 'name' => 'A')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '3.html', 'name' => 'B')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '4.html')));
+ $page->acceptFramesetEnd();
+
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array(
+ 1 => new SimpleUrl('http://here/1.html'),
+ 'A' => new SimpleUrl('http://here/2.html'),
+ 'B' => new SimpleUrl('http://here/3.html'),
+ 4 => new SimpleUrl('http://here/4.html')));
+ }
+ }
+
+ class TestOfForms extends UnitTestCase {
+
+ function testButtons() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->acceptFormStart(
+ new SimpleFormTag(array("method" => "GET", "action" => "here.php")));
+ $page->AcceptTag(
+ new SimpleSubmitTag(array("type" => "submit", "name" => "s")));
+ $page->acceptFormEnd();
+ $form = &$page->getFormBySubmitLabel("Submit");
+ $this->assertEqual(
+ $form->submitButtonByLabel("Submit"),
+ new SimpleFormEncoding(array("s" => "Submit")));
+ }
+ }
+
+ class TestOfPageScraping extends UnitTestCase {
+
+ function &parse($response) {
+ $builder = &new SimplePageBuilder();
+ return $builder->parse($response);
+ }
+
+ function testEmptyPage() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $this->assertIdentical($page->getAbsoluteUrls(), array());
+ $this->assertIdentical($page->getRelativeUrls(), array());
+ $this->assertIdentical($page->getTitle(), false);
+ }
+
+ function testUninterestingPage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'Stuff
');
+
+ $page = &$this->parse($response);
+ $this->assertIdentical($page->getAbsoluteUrls(), array());
+ $this->assertIdentical($page->getRelativeUrls(), array());
+ }
+
+ function testLinksPage() {
+ $raw = '';
+ $raw .= 'There';
+ $raw .= 'That page';
+ $raw .= '';
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', $raw);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://www.here.com/a/index.html'));
+
+ $page = &$this->parse($response);
+ $this->assertIdentical(
+ $page->getAbsoluteUrls(),
+ array('http://there.com/that.html'));
+ $this->assertIdentical(
+ $page->getRelativeUrls(),
+ array('there.html'));
+ $this->assertIdentical(
+ $page->getUrlsByLabel('There'),
+ array(new SimpleUrl('http://www.here.com/a/there.html')));
+ $this->assertEqual(
+ $page->getUrlById('0'),
+ new SimpleUrl('http://there.com/that.html'));
+ }
+
+ function testTitle() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'Me');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getTitle(), 'Me');
+ }
+
+ function testNastyTitle() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ ' Me&Me ');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getTitle(), "Me&Me");
+ }
+
+ function testCompleteForm() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent',
+ '');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('here'), "Hello");
+ }
+
+ function testUnclosedForm() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent',
+ '');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getFormBySubmitLabel('submit'));
+ $this->assertIsA($page->getFormBySubmitName('submit'), 'SimpleForm');
+ $this->assertIsA($page->getFormBySubmitLabel('Submit'), 'SimpleForm');
+ }
+
+ function testConfirmSubmitAttributesAreCaseInsensitive() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '');
+
+ $page = &$this->parse($response);
+ $this->assertIsA($page->getFormBySubmitName('submit'), 'SimpleForm');
+ $this->assertIsA($page->getFormBySubmitLabel('Submit'), 'SimpleForm');
+ }
+
+ function testFindFormByImage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertIsA($page->getFormByImageLabel('Label'), 'SimpleForm');
+ $this->assertIsA($page->getFormByImageName('me'), 'SimpleForm');
+ $this->assertIsA($page->getFormByImageId(100), 'SimpleForm');
+ }
+
+ function testFindFormByButtonTag() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getFormBySubmitLabel('b'));
+ $this->assertNull($page->getFormBySubmitLabel('B'));
+ $this->assertIsA($page->getFormBySubmitName('b'), 'SimpleForm');
+ $this->assertIsA($page->getFormBySubmitLabel('BBB'), 'SimpleForm');
+ }
+
+ function testFindFormById() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getFormById(54));
+ $this->assertIsA($page->getFormById(55), 'SimpleForm');
+ }
+
+ function testReadingTextField() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getField('missing'));
+ $this->assertIdentical($page->getField('a'), '');
+ $this->assertIdentical($page->getField('b'), 'bbb');
+ }
+
+ function testReadingTextFieldIsCaseInsensitive() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getField('missing'));
+ $this->assertIdentical($page->getField('a'), '');
+ $this->assertIdentical($page->getField('b'), 'bbb');
+ }
+
+ function testSettingTextField() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->setField('a', 'aaa'));
+ $this->assertEqual($page->getField('a'), 'aaa');
+ $this->assertTrue($page->setFieldById(3, 'bbb'));
+ $this->assertEqual($page->getFieldById(3), 'bbb');
+ $this->assertFalse($page->setField('z', 'zzz'));
+ $this->assertNull($page->getField('z'));
+ }
+
+ function testReadingTextArea() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('a'), 'aaa');
+ }
+
+ function testSettingTextArea() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->setField('a', 'AAA'));
+ $this->assertEqual($page->getField('a'), 'AAA');
+ }
+
+ function testSettingSelectionField() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('a'), 'bbb');
+ $this->assertFalse($page->setField('a', 'ccc'));
+ $this->assertTrue($page->setField('a', 'aaa'));
+ $this->assertEqual($page->getField('a'), 'aaa');
+ }
+ }
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/parse_error_test.php b/tests/UnitTests/simpletest/test/parse_error_test.php
new file mode 100644
index 00000000..dd0f4eae
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/parse_error_test.php
@@ -0,0 +1,10 @@
+addTestFile('test_with_parse_error.php');
+ $test->run(new HtmlReporter());
+?>
\ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/parser_test.php b/tests/UnitTests/simpletest/test/parser_test.php
new file mode 100644
index 00000000..6356c343
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/parser_test.php
@@ -0,0 +1,648 @@
+assertFalse($regex->match("Hello", $match));
+ $this->assertEqual($match, "");
+ }
+
+ function testNoSubject() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern(".*");
+ $this->assertTrue($regex->match("", $match));
+ $this->assertEqual($match, "");
+ }
+
+ function testMatchAll() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern(".*");
+ $this->assertTrue($regex->match("Hello", $match));
+ $this->assertEqual($match, "Hello");
+ }
+
+ function testCaseSensitive() {
+ $regex = &new ParallelRegex(true);
+ $regex->addPattern("abc");
+ $this->assertTrue($regex->match("abcdef", $match));
+ $this->assertEqual($match, "abc");
+ $this->assertTrue($regex->match("AAABCabcdef", $match));
+ $this->assertEqual($match, "abc");
+ }
+
+ function testCaseInsensitive() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern("abc");
+ $this->assertTrue($regex->match("abcdef", $match));
+ $this->assertEqual($match, "abc");
+ $this->assertTrue($regex->match("AAABCabcdef", $match));
+ $this->assertEqual($match, "ABC");
+ }
+
+ function testMatchMultiple() {
+ $regex = &new ParallelRegex(true);
+ $regex->addPattern("abc");
+ $regex->addPattern("ABC");
+ $this->assertTrue($regex->match("abcdef", $match));
+ $this->assertEqual($match, "abc");
+ $this->assertTrue($regex->match("AAABCabcdef", $match));
+ $this->assertEqual($match, "ABC");
+ $this->assertFalse($regex->match("Hello", $match));
+ }
+
+ function testPatternLabels() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern("abc", "letter");
+ $regex->addPattern("123", "number");
+ $this->assertIdentical($regex->match("abcdef", $match), "letter");
+ $this->assertEqual($match, "abc");
+ $this->assertIdentical($regex->match("0123456789", $match), "number");
+ $this->assertEqual($match, "123");
+ }
+ }
+
+ class TestOfStateStack extends UnitTestCase {
+
+ function testStartState() {
+ $stack = &new SimpleStateStack("one");
+ $this->assertEqual($stack->getCurrent(), "one");
+ }
+
+ function testExhaustion() {
+ $stack = &new SimpleStateStack("one");
+ $this->assertFalse($stack->leave());
+ }
+
+ function testStateMoves() {
+ $stack = &new SimpleStateStack("one");
+ $stack->enter("two");
+ $this->assertEqual($stack->getCurrent(), "two");
+ $stack->enter("three");
+ $this->assertEqual($stack->getCurrent(), "three");
+ $this->assertTrue($stack->leave());
+ $this->assertEqual($stack->getCurrent(), "two");
+ $stack->enter("third");
+ $this->assertEqual($stack->getCurrent(), "third");
+ $this->assertTrue($stack->leave());
+ $this->assertTrue($stack->leave());
+ $this->assertEqual($stack->getCurrent(), "one");
+ }
+ }
+
+ class TestParser {
+
+ function accept() {
+ }
+
+ function a() {
+ }
+
+ function b() {
+ }
+ }
+ Mock::generate('TestParser');
+
+ class TestOfLexer extends UnitTestCase {
+
+ function testEmptyPage() {
+ $handler = &new MockTestParser($this);
+ $handler->expectNever("accept");
+ $handler->setReturnValue("accept", true);
+ $handler->expectNever("accept");
+ $handler->setReturnValue("accept", true);
+ $lexer = &new SimpleLexer($handler);
+ $lexer->addPattern("a+");
+ $this->assertTrue($lexer->parse(""));
+ }
+
+ function testSinglePattern() {
+ $handler = &new MockTestParser($this);
+ $handler->expectArgumentsAt(0, "accept", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "accept", array("x", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "accept", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "accept", array("yyy", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "accept", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "accept", array("x", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(6, "accept", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(7, "accept", array("z", LEXER_UNMATCHED));
+ $handler->expectCallCount("accept", 8);
+ $handler->setReturnValue("accept", true);
+ $lexer = &new SimpleLexer($handler);
+ $lexer->addPattern("a+");
+ $this->assertTrue($lexer->parse("aaaxayyyaxaaaz"));
+ $handler->tally();
+ }
+
+ function testMultiplePattern() {
+ $handler = &new MockTestParser($this);
+ $target = array("a", "b", "a", "bb", "x", "b", "a", "xxxxxx", "a", "x");
+ for ($i = 0; $i < count($target); $i++) {
+ $handler->expectArgumentsAt($i, "accept", array($target[$i], '*'));
+ }
+ $handler->expectCallCount("accept", count($target));
+ $handler->setReturnValue("accept", true);
+ $lexer = &new SimpleLexer($handler);
+ $lexer->addPattern("a+");
+ $lexer->addPattern("b+");
+ $this->assertTrue($lexer->parse("ababbxbaxxxxxxax"));
+ $handler->tally();
+ }
+ }
+
+ class TestOfLexerModes extends UnitTestCase {
+
+ function testIsolatedPattern() {
+ $handler = &new MockTestParser($this);
+ $handler->expectArgumentsAt(0, "a", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("bxb", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "a", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "a", array("x", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(6, "a", array("aaaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(7, "a", array("x", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 8);
+ $handler->setReturnValue("a", true);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addPattern("b+", "b");
+ $this->assertTrue($lexer->parse("abaabxbaaaxaaaax"));
+ $handler->tally();
+ }
+
+ function testModeChange() {
+ $handler = &new MockTestParser($this);
+ $handler->expectArgumentsAt(0, "a", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "a", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(0, "b", array(":", LEXER_ENTER));
+ $handler->expectArgumentsAt(1, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "b", array("b", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "b", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(6, "b", array("bbb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(7, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 5);
+ $handler->expectCallCount("b", 8);
+ $handler->setReturnValue("a", true);
+ $handler->setReturnValue("b", true);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addEntryPattern(":", "a", "b");
+ $lexer->addPattern("b+", "b");
+ $this->assertTrue($lexer->parse("abaabaaa:ababbabbba"));
+ $handler->tally();
+ }
+
+ function testNesting() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->setReturnValue("b", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(0, "b", array("(", LEXER_ENTER));
+ $handler->expectArgumentsAt(1, "b", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(2, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(3, "b", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(4, "b", array(")", LEXER_EXIT));
+ $handler->expectArgumentsAt(4, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 6);
+ $handler->expectCallCount("b", 5);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addEntryPattern("(", "a", "b");
+ $lexer->addPattern("b+", "b");
+ $lexer->addExitPattern(")", "b");
+ $this->assertTrue($lexer->parse("aabaab(bbabb)aab"));
+ $handler->tally();
+ }
+
+ function testSingular() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->setReturnValue("b", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(2, "a", array("xx", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(3, "a", array("xx", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(0, "b", array("b", LEXER_SPECIAL));
+ $handler->expectArgumentsAt(1, "b", array("bbb", LEXER_SPECIAL));
+ $handler->expectCallCount("a", 4);
+ $handler->expectCallCount("b", 2);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addSpecialPattern("b+", "a", "b");
+ $this->assertTrue($lexer->parse("aabaaxxbbbxx"));
+ $handler->tally();
+ }
+
+ function testUnwindTooFar() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array(")", LEXER_EXIT));
+ $handler->expectCallCount("a", 2);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addExitPattern(")", "a");
+ $this->assertFalse($lexer->parse("aa)aa"));
+ $handler->tally();
+ }
+ }
+
+ class TestOfLexerHandlers extends UnitTestCase {
+
+ function testModeMapping() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("(", LEXER_ENTER));
+ $handler->expectArgumentsAt(2, "a", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "a", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "a", array(")", LEXER_EXIT));
+ $handler->expectArgumentsAt(6, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 7);
+ $lexer = &new SimpleLexer($handler, "mode_a");
+ $lexer->addPattern("a+", "mode_a");
+ $lexer->addEntryPattern("(", "mode_a", "mode_b");
+ $lexer->addPattern("b+", "mode_b");
+ $lexer->addExitPattern(")", "mode_b");
+ $lexer->mapHandler("mode_a", "a");
+ $lexer->mapHandler("mode_b", "a");
+ $this->assertTrue($lexer->parse("aa(bbabb)b"));
+ $handler->tally();
+ }
+ }
+
+ Mock::generate("HtmlSaxParser");
+
+ class TestOfHtmlLexer extends UnitTestCase {
+ var $_handler;
+ var $_lexer;
+
+ function setUp() {
+ $this->_handler = &new MockSimpleSaxParser($this);
+ $this->_handler->setReturnValue("acceptStartToken", true);
+ $this->_handler->setReturnValue("acceptEndToken", true);
+ $this->_handler->setReturnValue("acceptAttributeToken", true);
+ $this->_handler->setReturnValue("acceptEntityToken", true);
+ $this->_handler->setReturnValue("acceptTextToken", true);
+ $this->_handler->setReturnValue("ignore", true);
+ $this->_lexer = &SimpleSaxParser::createLexer($this->_handler);
+ }
+
+ function tearDown() {
+ $this->_handler->tally();
+ }
+
+ function testUninteresting() {
+ $this->_handler->expectOnce("acceptTextToken", array("", "*"));
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testSkipCss() {
+ $this->_handler->expectMaximumCallCount("acceptTextToken", 0);
+ $this->_handler->expectAtLeastOnce("ignore");
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testSkipJavaScript() {
+ $this->_handler->expectMaximumCallCount("acceptTextToken", 0);
+ $this->_handler->expectAtLeastOnce("ignore");
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testSkipComments() {
+ $this->_handler->expectMaximumCallCount("acceptTextToken", 0);
+ $this->_handler->expectAtLeastOnce("ignore");
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testTitleTag() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptTextToken", array("Hello", "*"));
+ $this->_handler->expectOnce("acceptEndToken", array("", "*"));
+ $this->assertTrue($this->_lexer->parse("Hello"));
+ }
+
+ function testFramesetTag() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("", "*"));
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testInputTag() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array("name", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array("value", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("=a.b.c", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("= d", "*"));
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testEmptyLink() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptEndToken", array("", "*"));
+ $this->assertTrue($this->_lexer->parse(""));
+ }
+
+ function testLabelledLink() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptEndToken", array("", "*"));
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->assertTrue($this->_lexer->parse("label"));
+ }
+
+ function testLinkAddress() {
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array("href", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("= '", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("here.html", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("'", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 3);
+ $this->assertTrue($this->_lexer->parse("label"));
+ }
+
+ function testEncodedLinkAddress() {
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array("href", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("= '", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("here&there.html", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("'", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 3);
+ $this->assertTrue($this->_lexer->parse("label"));
+ }
+
+ function testEmptyLinkWithId() {
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array("id", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("=\"", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("0", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("\"", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 3);
+ $this->assertTrue($this->_lexer->parse("label"));
+ }
+
+ function testComplexLink() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array("HREF", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array("bool", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptStartToken", array("Style", "*"));
+ $this->_handler->expectArgumentsAt(4, "acceptStartToken", array(">", LEXER_EXIT));
+ $this->_handler->expectCallCount("acceptStartToken", 5);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("= '", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("here.html", LEXER_UNMATCHED));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("'", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptAttributeToken", array("=\"", "*"));
+ $this->_handler->expectArgumentsAt(4, "acceptAttributeToken", array("'coo", "*"));
+ $this->_handler->expectArgumentsAt(5, "acceptAttributeToken", array('\"', "*"));
+ $this->_handler->expectArgumentsAt(6, "acceptAttributeToken", array("l'", "*"));
+ $this->_handler->expectArgumentsAt(7, "acceptAttributeToken", array("\"", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 8);
+ $this->assertTrue($this->_lexer->parse("label"));
+ }
+
+ function testSubmit() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("_handler->expectArgumentsAt(1, "acceptStartToken", array("type", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array("name", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptStartToken", array("value", "*"));
+ $this->_handler->expectArgumentsAt(4, "acceptStartToken", array("/", "*"));
+ $this->_handler->expectArgumentsAt(5, "acceptStartToken", array(">", LEXER_EXIT));
+ $this->_handler->expectCallCount("acceptStartToken", 6);
+ $this->assertTrue($this->_lexer->parse(''));
+ }
+
+ function testFramesParsedWithoutError() {
+ $this->assertTrue($this->_lexer->parse(
+ ''));
+ $this->assertTrue($this->_lexer->parse(
+ ''));
+ }
+ }
+
+ class TestOfTextExtraction extends UnitTestCase {
+
+ function testSpaceNormalisation() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise("\nOne\tTwo \nThree\t"),
+ 'One Two Three');
+ }
+
+ function testTagSuppression() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('Hello'),
+ 'Hello');
+ }
+
+ function testAdjoiningTagSuppression() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('HelloGoodbye'),
+ 'HelloGoodbye');
+ }
+
+ function testExtractImageAltTextWithDifferentQuotes() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('![One]()
![\'Two\']()
'),
+ 'One Two Three');
+ }
+
+ function testExtractImageAltTextMultipleTimes() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('![One]()
![Two]()
'),
+ 'One Two Three');
+ }
+
+ function testHtmlEntityTranslation() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('<>"&'),
+ '<>"&');
+ }
+ }
+
+ class TestSimpleSaxParser extends SimpleSaxParser {
+ var $_lexer;
+
+ function TestSimpleSaxParser(&$listener, &$lexer) {
+ $this->_lexer = &$lexer;
+ $this->SimpleSaxParser($listener);
+ }
+
+ function &createLexer() {
+ return $this->_lexer;
+ }
+ }
+
+ Mock::generate("SimpleSaxListener");
+ Mock::generate("SimpleLexer");
+
+ class TestOfSaxGeneration extends UnitTestCase {
+ var $_listener;
+ var $_lexer;
+
+ function setUp() {
+ $this->_listener = &new MockSimpleSaxListener($this);
+ $this->_lexer = &new MockSimpleLexer($this);
+ $this->_parser = &new TestSimpleSaxParser($this->_listener, $this->_lexer);
+ }
+
+ function tearDown() {
+ $this->_listener->tally();
+ $this->_lexer->tally();
+ }
+
+ function testLexerFailure() {
+ $this->_lexer->setReturnValue("parse", false);
+ $this->assertFalse($this->_parser->parse(""));
+ }
+
+ function testLexerSuccess() {
+ $this->_lexer->setReturnValue("parse", true);
+ $this->assertTrue($this->_parser->parse(""));
+ }
+
+ function testSimpleLinkStart() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("startElement", array("a", array()));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testSimpleTitleStart() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("startElement", array("title", array()));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkStart() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("startElement", array("a", array("href" => "here.html")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("assertTrue($this->_parser->acceptStartToken("href", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("=\"", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptAttributeToken("here.html", LEXER_UNMATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("\"", LEXER_EXIT));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkStartWithEncodedUrl() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("a", array("href" => "here&there.html")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("assertTrue($this->_parser->acceptStartToken("href", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("=\"", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptAttributeToken("here&there.html", LEXER_UNMATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("\"", LEXER_EXIT));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkStartWithId() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("a", array("id" => "0")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("assertTrue($this->_parser->acceptStartToken("id", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("= \"", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptAttributeToken("0", LEXER_UNMATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("\"", LEXER_EXIT));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkEnd() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("endElement", array("a"));
+ $this->_listener->setReturnValue("endElement", true);
+ $this->assertTrue($this->_parser->acceptEndToken("", LEXER_SPECIAL));
+ }
+
+ function testInput() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("input", array("name" => "a")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("assertTrue($this->_parser->acceptStartToken("name", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("= a", LEXER_SPECIAL));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testButton() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("button", array("name" => "a")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("