page_test.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 903 行 · 第 1/3 页

PHP
903
字号
    }

    function testAddBareRelativeLink() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
        $page = &new SimplePage($response);
        $page->AcceptTag(new SimpleAnchorTag(array('href' => 'somewhere.php')));
        $this->assertIdentical($page->getUrls(), array('http://host/somewhere.php'));
    }

    function testAddRelativeLinkWithBaseTag() {
        $link = &new SimpleAnchorTag(array('href' => 'somewhere.php'));
        $link->addContent('Label');
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
        $page = &new SimplePage($response);
        $page->AcceptTag($link);        
        $base = &new SimpleBaseTag(array('href' => 'www.lastcraft.com/stuff/'));
        $page->AcceptTag($base);
        $this->assertEqual(
                $page->getUrlsByLabel('Label'),
                array(new SimpleUrl('www.lastcraft.com/stuff/somewhere.php')));
    }

    function testAddAbsoluteLinkWithBaseTag() {
        $link = &new SimpleAnchorTag(array('href' => 'http://here.com/somewhere.php'));
        $link->addContent('Label');
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
        $page = &new SimplePage($response);
        $page->AcceptTag($link);        
        $base = &new SimpleBaseTag(array('href' => 'www.lastcraft.com/stuff/'));
        $page->AcceptTag($base);
        $this->assertEqual(
                $page->getUrlsByLabel('Label'),
                array(new SimpleUrl('http://here.com/somewhere.php')));
    }

    function testLinkIds() {
        $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
        $link->addContent('Label');

        $response = &new MockSimpleHttpResponse();
        $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(' <em>Long &amp; thin</em> ');

        $response = &new MockSimpleHttpResponse();
        $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('<img src="pic.jpg" alt="&lt;A picture&gt;">');

        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));

        $page = &new SimplePage($response);
        $page->AcceptTag($link);

        $this->assertEqual(
                $page->getUrlsByLabel('<A picture>'),
                array(new SimpleUrl('http://host/somewhere.php')));
    }

    function testTitleSetting() {
        $title = &new SimpleTitleTag(array());
        $title->addContent('Title');
        $page = &new SimplePage(new MockSimpleHttpResponse());
        $page->AcceptTag($title);
        $this->assertEqual($page->getTitle(), 'Title');
    }

    function testFramesetAbsence() {
        $url = new SimpleUrl('here');
        $response = new MockSimpleHttpResponse();
        $response->setReturnValue('getUrl', $url);
        $page = &new SimplePage($response);
        $this->assertFalse($page->hasFrames());
        $this->assertIdentical($page->getFrameset(), false);
    }

    function testHasEmptyFrameset() {
        $page = &new SimplePage(new MockSimpleHttpResponse());
        $page->acceptFramesetStart(new SimpleTag('frameset', array()));
        $page->acceptFramesetEnd();
        $this->assertTrue($page->hasFrames());
        $this->assertIdentical($page->getFrameset(), array());
    }

    function testFramesInPage() {
        $response = &new MockSimpleHttpResponse();
        $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();
        $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')));
    }
    
    function testRelativeFramesRespectBaseTag() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getUrl', new SimpleUrl('http://here.com/'));
        $page = &new SimplePage($response);

        $base = &new SimpleBaseTag(array('href' => 'https://there.com/stuff/'));
        $page->AcceptTag($base);

        $page->acceptFramesetStart(new SimpleTag('frameset', array()));
        $page->acceptFrame(new SimpleFrameTag(array('src' => '1.html')));
        $page->acceptFramesetEnd();
        $this->assertIdentical(
                $page->getFrameset(),
                array(1 => new SimpleUrl('https://there.com/stuff/1.html')));
    }
}

class TestOfFormsCreatedFromEventStream extends UnitTestCase {

    function testFormCanBeSubmitted() {
        $page = &new SimplePage(new MockSimpleHttpResponse());
        $page->acceptFormStart(
                new SimpleFormTag(array('method' => 'GET', 'action' => 'here.php')));
        $page->AcceptTag(
                new SimpleSubmitTag(array('type' => 'submit', 'name' => 's')));
        $page->acceptFormEnd();
        $form = &$page->getFormBySubmit(new SimpleByLabel('Submit'));
        $this->assertEqual(
                $form->submitButton(new SimpleByLabel('Submit')),
                new SimpleGetEncoding(array('s' => 'Submit')));
    }

    function testInputFieldCanBeReadBack() {
        $page = &new SimplePage(new MockSimpleHttpResponse());
        $page->acceptFormStart(
                new SimpleFormTag(array("method" => "GET", "action" => "here.php")));
        $page->AcceptTag(
                new SimpleTextTag(array("type" => "text", "name" => "a", "value" => "A")));
        $page->AcceptTag(
                new SimpleSubmitTag(array("type" => "submit", "name" => "s")));
        $page->acceptFormEnd();
        $this->assertEqual($page->getField(new SimpleByName('a')), 'A');
    }

    function testInputFieldCanBeReadBackByLabel() {
        $label = &new SimpleLabelTag(array());
        $page = &new SimplePage(new MockSimpleHttpResponse());
        $page->acceptFormStart(
                new SimpleFormTag(array("method" => "GET", "action" => "here.php")));
        $page->acceptLabelStart($label);
        $label->addContent('l');
        $page->AcceptTag(
                new SimpleTextTag(array("type" => "text", "name" => "a", "value" => "A")));
        $page->acceptLabelEnd();
        $page->AcceptTag(
                new SimpleSubmitTag(array("type" => "submit", "name" => "s")));
        $page->acceptFormEnd();
        $this->assertEqual($page->getField(new SimpleByLabel('l')), 'A');
    }
}

class TestOfPageScraping extends UnitTestCase {

    function &parse($response) {
        $builder = &new SimplePageBuilder();
        $page = &$builder->parse($response);
        return $page;
    }

    function testEmptyPage() {
        $page = &new SimplePage(new MockSimpleHttpResponse());
        $this->assertIdentical($page->getUrls(), array());
        $this->assertIdentical($page->getTitle(), false);
    }

    function testUninterestingPage() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getContent', '<html><body><p>Stuff</p></body></html>');
        $page = &$this->parse($response);
        $this->assertIdentical($page->getUrls(), array());
    }

    function testLinksPage() {
        $raw = '<html>';
        $raw .= '<a href="there.html">There</a>';
        $raw .= '<a href="http://there.com/that.html" id="0">That page</a>';
        $raw .= '</html>';
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getContent', $raw);
        $response->setReturnValue('getUrl', new SimpleUrl('http://www.here.com/a/index.html'));

        $page = &$this->parse($response);
        $this->assertIdentical(
                $page->getUrls(),
                array('http://www.here.com/a/there.html', 'http://there.com/that.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();
        $response->setReturnValue('getContent', '<html><head><title>Me</title></head></html>');
        $page = &$this->parse($response);
        $this->assertEqual($page->getTitle(), 'Me');
    }

    function testNastyTitle() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue(
                'getContent',
                '<html><head><Title> <b>Me&amp;Me </TITLE></b></head></html>');
        $page = &$this->parse($response);
        $this->assertEqual($page->getTitle(), "Me&Me");
    }

    function testCompleteForm() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getContent',
                '<html><head><form>' .
                '<input type="text" name="here" value="Hello">' .
                '</form></head></html>');
        $page = &$this->parse($response);
        $this->assertEqual($page->getField(new SimpleByName('here')), "Hello");
    }

    function testUnclosedForm() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue('getContent',
                '<html><head><form>' .
                '<input type="text" name="here" value="Hello">' .
                '</head></html>');
        $page = &$this->parse($response);
        $this->assertEqual($page->getField(new SimpleByName('here')), "Hello");
    }

    function testEmptyFrameset() {
        $response = &new MockSimpleHttpResponse();
        $response->setReturnValue(
                'getContent',
                '<html><frameset></frameset></html>');
        $page = &$this->parse($response);
        $this->assertTrue($page->hasFrames());
        $this->assertIdentical($page->getFrameset(), array());
    }

    function testSingleFrame() {
        $response = &new MockSimpleHttpResponse();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?