Perforce Chronicle 2012.2/486814
API Documentation

Search_Test_IndexControllerTest Class Reference

Test the content index controller. More...

List of all members.

Public Member Functions

 setUp ()
 Perform setup.
 testGoodQueryPost ()
 Test the search queries.
 testIndex ()
 Test view action.
 testPaginationUrl ()
 Test url format in the pagination.

Public Attributes

 $bootstrap = array('Bootstrap', 'run')

Protected Member Functions

 _createContent ()
 Create several test content entries.

Detailed Description

Test the content index controller.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

Search_Test_IndexControllerTest::_createContent ( ) [protected]

Create several test content entries.

    {
        // test type content
        $entry = new P4Cms_Content;
        $entry->setId('test123')
              ->setContentType('test-type')
              ->setValue('title', 'My Title')
              ->setValue('body',  'My content body')
              ->save();

        // test type w. id content
        $entry = new P4Cms_Content;
        $entry->setId('test456')
              ->setContentType('test-type-w-id')
              ->setValue('title', 'My Title')
              ->setValue('body',  'My content body')
              ->save();

        // test type w. file
        $entry = new P4Cms_Content;
        $entry->setId('test789')
              ->setContentType('test-type-w-file')
              ->setValue('title', 'My Title')
              ->setValue('file',  'test file content')
              ->setFieldMetadata(
                'file',
                array('filename' => 'myfile.txt', 'mimeType' => 'text/plain')
              )
              ->save();

        // test type w. image
        $entry = new P4Cms_Content;
        $entry->setId('test5309')
              ->setContentType('test-type-w-file')
              ->setValue('title', 'Test Image')
              ->setValue('file',  'test image content')
              ->setFieldMetadata(
                'file',
                array('filename' => 'image.jpg', 'mimeType' => 'image/jpg')
              )
              ->save();
    }
Search_Test_IndexControllerTest::setUp ( )

Perform setup.

    {
        parent::setUp();

        $searchModule = P4Cms_Module::fetch('Search');
        $searchModule->enable();
        $searchModule->load();
        P4Cms_Content_Type::installDefaultTypes();

        // install default ACL
        $acl = P4Cms_Site::fetchActive()->getAcl();
        $acl->installDefaults()->save();

        // ensure a type is present for testing.
        $type = new P4Cms_Content_Type;
        $type->setId('test-type')
             ->setLabel('Test Type')
             ->setElements(
                array(
                    "title" => array(
                        "type"      => "text",
                        "options"   => array("label" => "Title", "required" => true)
                    ),
                    "body"  => array(
                        "type"      => "textarea",
                        "options"   => array("label" => "Body")
                    )
                )
             )
             ->setValue('icon', file_get_contents(TEST_ASSETS_PATH . "/images/content-type-icon.png"))
             ->setFieldMetadata('icon', array("mimeType" => "image/png"))
             ->setValue('group', 'test')
             ->save();

        // ensure a type w. id is present for testing.
        $type = new P4Cms_Content_Type;
        $type->setId('test-type-w-id')
             ->setLabel('Test Type')
             ->setElements(
                array(
                    "id" => array(
                        "type"      => "text",
                        "options"   => array("label" => "Title", "required" => true)
                    ),
                    "title" => array(
                        "type"      => "text",
                        "options"   => array("label" => "Title", "required" => true)
                    ),
                    "body"  => array(
                        "type"      => "textarea",
                        "options"   => array("label" => "Body")
                    )
                )
             )
             ->setValue('icon', file_get_contents(TEST_ASSETS_PATH . "/images/content-type-icon.png"))
             ->setFieldMetadata('icon', array("mimeType" => "image/png"))
             ->setValue('group', 'test')
             ->save();

        // ensure a type w. a file is present for testing.
        $type = new P4Cms_Content_Type;
        $type->setId('test-type-w-file')
             ->setLabel('Test Type')
             ->setElements(
                array(
                    "title" => array(
                        "type"      => "text",
                        "options"   => array("label" => "Title", "required" => true)
                    ),
                    "name"  => array(
                        "type"      => "file",
                        "options"   => array("label" => "File")
                    )
                )
             )
             ->setValue('icon', file_get_contents(TEST_ASSETS_PATH . "/images/content-type-icon.png"))
             ->setFieldMetadata('icon', array("mimeType" => "image/png"))
             ->setValue('group', 'test2')
             ->save();
    }
Search_Test_IndexControllerTest::testGoodQueryPost ( )

Test the search queries.

    {
        $this->utility->impersonate('anonymous');

        // make sure there are contents to search
        $this->_createContent();

        //test 1
        $query = 'body';
        $params = array(
            'query'          => $query,
        );
        $this->request->setMethod('POST');
        $this->request->setPost($params);
        $this->dispatch('/search');
        $responseBody = $this->response->getBody();
        $this->assertModule('search', 'Last module run should be content module.'. $responseBody);
        $this->assertController('index', 'Expected controller.'. $responseBody);
        $this->assertAction('index', 'Expected action.'. $responseBody);

        $this->assertResponseCode(200, 'Expected response code.'. $responseBody);
        $this->assertRegexp('#/id/test123#', $responseBody, 'Expected content id');
        $this->assertRegexp('#/id/test456#', $responseBody, 'Expected content id');

        //test 2
        $query = 'empty';
        $params = array(
            'query'          => $query,
        );
        $this->request->setMethod('POST');
        $this->request->setPost($params);
        $this->dispatch('/search');
        $responseBody = $this->response->getBody();
        $this->assertModule('search', 'Last module run should be content module.'. $responseBody);
        $this->assertController('index', 'Expected controller.'. $responseBody);
        $this->assertAction('index', 'Expected action.'. $responseBody);

        $this->assertResponseCode(200, 'Expected response code.'. $responseBody);
        $this->assertRegexp('/No matching documents./', $responseBody, 'Expected empty search results.');

    }
Search_Test_IndexControllerTest::testIndex ( )

Test view action.

    {
        $this->utility->impersonate('anonymous');

        $this->dispatch('/search/index');
        $body = $this->response->getBody();
        $this->assertModule('search', 'Last module run should be content module.'. $body);
        $this->assertController('index', 'Expected controller'. $body);
        $this->assertAction('index', 'Expected action'. $body);

        // check that output looks sane.
        $this->assertQueryContentRegex(
            '#content h1',
            '/Search/',
            'Expect the page title.'
        );
    }
Search_Test_IndexControllerTest::testPaginationUrl ( )

Test url format in the pagination.

    {
        // create several content entries
        for ($i = 1; $i <= 21; $i++) {
            $entry = new P4Cms_Content;
            $entry->setId("test$i")
                  ->setContentType('test-type-w-id')
                  ->setValue('title', "test $i")
                  ->setValue('body',  "body $i")
                  ->save();
        }

        $this->request->setMethod('GET');
        $this->request->setQuery(
            array(
                'query'  => 'test body',
                'page'   => '2'
            )
        );
        $this->dispatch('/search');
        $body = $this->response->getBody();

        $this->assertModule('search', 'Expected last module: ' . $body);
        $this->assertController('index', 'Expected last controller: ' . $body);
        $this->assertAction('index', 'Expected action: ' . $body);

        // check particular page link
        $this->assertQueryContentContains(
            "div.paginationControl a[href=\"/search?query=test+body&page=1\"]",
            '1'
        );

        // check previous page link
        $this->assertQueryContentContains(
            "div.paginationControl a[href=\"/search?query=test+body&page=1\"]",
            'Previous'
        );

        // check next page link
        $this->assertQueryContentContains(
            "div.paginationControl a[href=\"/search?query=test+body&page=3\"]",
            'Next'
        );
    }

Member Data Documentation

Search_Test_IndexControllerTest::$bootstrap = array('Bootstrap', 'run')

The documentation for this class was generated from the following file: