public function testGetBySelector() { $parser = new CSSContentParser(<<<HTML <html> \t<head> \t\t<title>test</title> \t</head> \t<body> \t\t<div id="A" class="one two three"> \t\t\t<p class="other">result</p> \t\t</div> \t\t<p>test</p> \t</body> </html> HTML ); $result = $parser->getBySelector('div.one'); $this->assertEquals("A", $result[0]['id'] . ''); $result = $parser->getBySelector('div.two'); $this->assertEquals("A", $result[0]['id'] . ''); $result = $parser->getBySelector('div.three'); $this->assertEquals("A", $result[0]['id'] . ''); $result = $parser->getBySelector('div#A p.other'); $this->assertEquals("result", $result[0] . ''); $result = $parser->getBySelector('#A .other'); $this->assertEquals("result", $result[0] . ''); }
public function testSetDisabledItems() { $f = new OptionsetField('Test', false, array(0 => 'Zero', 1 => 'One')); $f->setDisabledItems(array(0)); $p = new CSSContentParser($f->Field()); $item0 = $p->getBySelector('#Test_0'); $item1 = $p->getBySelector('#Test_1'); $this->assertEquals((string) $item0[0]['disabled'], 'disabled'); $this->assertEquals((string) $item1[0]['disabled'], ''); }
public function testLegend() { $composite = new CompositeField(new TextField('A'), new TextField('B')); $composite->setTag('fieldset'); $composite->setLegend('My legend'); $parser = new CSSContentParser($composite->FieldHolder()); $root = $parser->getBySelector('fieldset.composite'); $legend = $parser->getBySelector('fieldset.composite legend'); $this->assertNotNull($legend); $this->assertEquals('My legend', (string) $legend[0]); }
public function testThereIsNoPaginatorWhenOnlyOnePage() { // We set the itemsPerPage to an reasonably big number so as to avoid test broke from small changes // on the fixture YML file $total = $this->list->count(); $this->gridField->getConfig()->getComponentByType("SilverStripe\\Forms\\GridField\\GridFieldPaginator")->setItemsPerPage($total); $fieldHolder = $this->gridField->FieldHolder(); $content = new CSSContentParser($fieldHolder); // Check that there is no paginator render into the footer $this->assertEquals(0, count($content->getBySelector('.datagrid-pagination'))); // Check that there is still 'View 1 - 4 of 4' part on the left of the paginator $this->assertEquals(2, count($content->getBySelector('.pagination-records-number'))); }
public function testSetDefaultItems() { $f = new CheckboxSetField('Test', false, array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => 'Three')); $f->setValue(array(0, 1)); $f->setDefaultItems(array(2)); $p = new CSSContentParser($f->Field()); $item0 = $p->getBySelector('#Test_0'); $item1 = $p->getBySelector('#Test_1'); $item2 = $p->getBySelector('#Test_2'); $item3 = $p->getBySelector('#Test_3'); $this->assertEquals((string) $item0[0]['checked'], 'checked', 'Selected through value'); $this->assertEquals((string) $item1[0]['checked'], 'checked', 'Selected through value'); $this->assertEquals((string) $item2[0]['checked'], 'checked', 'Selected through default items'); $this->assertEquals((string) $item3[0]['checked'], '', 'Not selected by either value or default items'); }
public function testShowEditLinksWithAdminPermission() { $this->logInWithPermission('ADMIN'); $content = new CSSContentParser($this->gridField->FieldHolder()); $editLinks = $content->getBySelector('.edit-link'); $this->assertEquals(2, count($editLinks), 'Edit links should show when logged in.'); }
public function testShowDeleteButtonsWithAdminPermission() { $this->logInWithPermission('ADMIN'); $content = new CSSContentParser($this->gridField->FieldHolder()); $deleteButtons = $content->getBySelector('.gridfield-button-delete'); $this->assertEquals(3, count($deleteButtons), 'Delete buttons should show when logged in.'); }
public function testTreeSearch() { $field = new TreeDropdownField('TestTree', 'Test tree', 'SilverStripe\\Assets\\Folder'); // case insensitive search against keyword 'sub' for folders $request = new HTTPRequest('GET', 'url', array('search' => 'sub')); $tree = $field->tree($request); $folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1'); $folder1Subfolder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1-subfolder1'); $parser = new CSSContentParser($tree); $cssPath = 'ul.tree li#selector-TestTree-' . $folder1->ID . ' li#selector-TestTree-' . $folder1Subfolder1->ID . ' a span.item'; $firstResult = $parser->getBySelector($cssPath); $this->assertEquals((string) $firstResult[0], $folder1Subfolder1->Name, $folder1Subfolder1->Name . ' is found, nested under ' . $folder1->Name); $subfolder = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'subfolder'); $cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' a span.item'; $secondResult = $parser->getBySelector($cssPath); $this->assertEquals((string) $secondResult[0], $subfolder->Name, $subfolder->Name . ' is found at root level'); // other folders which don't contain the keyword 'sub' are not returned in search results $folder2 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder2'); $cssPath = 'ul.tree li#selector-TestTree-' . $folder2->ID . ' a span.item'; $noResult = $parser->getBySelector($cssPath); $this->assertEquals($noResult, array(), $folder2 . ' is not found'); $field = new TreeDropdownField('TestTree', 'Test tree', 'SilverStripe\\Assets\\File'); // case insensitive search against keyword 'sub' for files $request = new HTTPRequest('GET', 'url', array('search' => 'sub')); $tree = $field->tree($request); $parser = new CSSContentParser($tree); // Even if we used File as the source object, folders are still returned because Folder is a File $cssPath = 'ul.tree li#selector-TestTree-' . $folder1->ID . ' li#selector-TestTree-' . $folder1Subfolder1->ID . ' a span.item'; $firstResult = $parser->getBySelector($cssPath); $this->assertEquals((string) $firstResult[0], $folder1Subfolder1->Name, $folder1Subfolder1->Name . ' is found, nested under ' . $folder1->Name); // Looking for two files with 'sub' in their name, both under the same folder $file1 = $this->objFromFixture('SilverStripe\\Assets\\File', 'subfolderfile1'); $file2 = $this->objFromFixture('SilverStripe\\Assets\\File', 'subfolderfile2'); $cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' li#selector-TestTree-' . $file1->ID . ' a'; $firstResult = $parser->getBySelector($cssPath); $this->assertGreaterThan(0, count($firstResult), $file1->Name . ' with ID ' . $file1->ID . ' is in search results'); $this->assertEquals((string) $firstResult[0], $file1->Name, $file1->Name . ' is found nested under ' . $subfolder->Name); $cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' li#selector-TestTree-' . $file2->ID . ' a'; $secondResult = $parser->getBySelector($cssPath); $this->assertGreaterThan(0, count($secondResult), $file2->Name . ' with ID ' . $file2->ID . ' is in search results'); $this->assertEquals((string) $secondResult[0], $file2->Name, $file2->Name . ' is found nested under ' . $subfolder->Name); // other files which don't include 'sub' are not returned in search results $file3 = $this->objFromFixture('SilverStripe\\Assets\\File', 'asdf'); $cssPath = 'ul.tree li#selector-TestTree-' . $file3->ID; $noResult = $parser->getBySelector($cssPath); $this->assertEquals($noResult, array(), $file3->Name . ' is not found'); }
function testLegacyItemsFieldHolderWithTitle() { $items = array('one//one title' => new LiteralField('one', 'one view'), 'two//two title' => new LiteralField('two', 'two view')); $field = new SelectionGroup('MyGroup', $items); $parser = new CSSContentParser($field->FieldHolder()); $listEls = $parser->getBySelector('li'); $listElOne = $listEls[0]; $listElTwo = $listEls[1]; $this->assertEquals('one', (string) $listElOne->input[0]['value']); $this->assertEquals('two', (string) $listElTwo->input[0]['value']); $this->assertEquals('one title', (string) $listElOne->label[0]); $this->assertEquals('two title', (string) $listElTwo->label[0]); }
public function testDateFormatCustomFormatAppearsInCustomInputInField() { $member = $this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember'); $member->setField('DateFormat', 'dd MM yy'); $field = $this->createDateFormatFieldForMember($member); /** @skipUpgrade */ $field->setForm(new Form(new MemberDatetimeOptionsetFieldTest_Controller(), 'Form', new FieldList(), new FieldList())); // fake form $parser = new CSSContentParser($field->Field()); $xmlInputArr = $parser->getBySelector('.valcustom input'); $this->assertEquals('checked', (string) $xmlInputArr[0]['checked']); $this->assertEquals('dd MM yy', (string) $xmlInputArr[1]['value']); }
public function testAdd() { $this->logInWithPermission('ADMIN'); $team1 = $this->objFromFixture('GridFieldTest_Team', 'team1'); $team2 = $this->objFromFixture('GridFieldTest_Team', 'team2'); $response = $this->get('GridFieldAddExistingAutocompleterTest_Controller'); $this->assertFalse($response->isError()); $parser = new CSSContentParser($response->getBody()); $items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item'); $this->assertEquals(1, count($items)); $this->assertEquals($team1->ID, (int) $items[0]['data-id']); $btns = $parser->getBySelector('.grid-field .action_gridfield_relationadd'); $response = $this->post('GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield', array('relationID' => $team2->ID, (string) $btns[0]['name'] => 1)); $this->assertFalse($response->isError()); $parser = new CSSContentParser($response->getBody()); $items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item'); $this->assertEquals(2, count($items)); $this->assertDOSEquals(array(array('ID' => (int) $items[0]['data-id']), array('ID' => (int) $items[1]['data-id'])), new ArrayList(array($team1, $team2))); }
/** * Test control output html */ public function testView() { $this->logInWithPermission('ADMIN'); $record = $this->objFromFixture('AssetFieldTest_Object', 'object1'); // Requesting form is not an error $response = $this->get('AssetFieldTest_Controller'); $this->assertFalse($response->isError()); // File exists in this response $parser = new CSSContentParser($response->getBody()); $tuple = array(); $result = $parser->getBySelector("#AssetFieldTest_Form_Form_File_Holder .ss-uploadfield-files .ss-uploadfield-item input[type='hidden']"); foreach ($result as $part) { $name = (string) $part['name']; $value = (string) $part['value']; switch ($name) { case 'File[Filename]': $tuple['Filename'] = $value; break; case 'File[Hash]': $tuple['Hash'] = $value; break; case 'File[Variant]': $tuple['Variant'] = $value; break; } } // Assert this value is correct $expected = array('Filename' => 'MyFiles/subfolder1/file-subfolder.txt', 'Hash' => '55b443b60176235ef09801153cca4e6da7494a0c', 'Variant' => ''); $this->assertEquals($expected, $record->File->getValue()); $this->assertEquals($expected, $tuple); }
public function testFieldRenderingMultipleOn() { $choices = array('a' => 'a value', 'b' => 'b value', 'c' => 'c value'); $field = new ListboxField('Choices', 'Choices', $choices); $field->setValue(array('a', 'c')); $parser = new CSSContentParser($field->Field()); $optEls = $parser->getBySelector('option'); $this->assertEquals(3, count($optEls)); $this->assertEquals('selected', (string) $optEls[0]['selected']); $this->assertEquals('', (string) $optEls[1]['selected']); $this->assertEquals('selected', (string) $optEls[2]['selected']); }
/** * Find all the <OPTION> elements from a * string of HTML. * * @param string $html HTML to scan for elements * @return SimpleXMLElement */ public function findOptionElements($html) { $parser = new CSSContentParser($html); return $parser->getBySelector('option'); }
/** * @covers SilverStripe\Forms\GridField\GridField::FieldHolder */ public function testCanViewOnlyOddIDs() { $this->logInWithPermission(); $list = new ArrayList(array(new GridFieldTest_Permissions(array("ID" => 1, "Email" => "*****@*****.**", 'Name' => 'Ongi Schwimmer')), new GridFieldTest_Permissions(array("ID" => 2, "Email" => "*****@*****.**", 'Name' => 'Klaus Lozenge')), new GridFieldTest_Permissions(array("ID" => 3, "Email" => "*****@*****.**", 'Name' => 'Otto Fischer')))); $config = new GridFieldConfig(); $config->addComponent(new GridFieldDataColumns()); $obj = new GridField('testfield', 'testfield', $list, $config); $form = new Form(new Controller(), 'mockform', new FieldList(array($obj)), new FieldList()); $content = new CSSContentParser($obj->FieldHolder()); $members = $content->getBySelector('.ss-gridfield-item tr'); $this->assertEquals(2, count($members)); $this->assertEquals((string) $members[0]->td[0], 'Ongi Schwimmer', 'First object Name should be Ongi Schwimmer'); $this->assertEquals((string) $members[0]->td[1], '*****@*****.**', 'First object Email should be ongi.schwimmer@example.org'); $this->assertEquals((string) $members[1]->td[0], 'Otto Fischer', 'Second object Name should be Otto Fischer'); $this->assertEquals((string) $members[1]->td[1], '*****@*****.**', 'Second object Email should be otto.fischer@example.org'); }
/** * Tests that a has-many detail form is pre-populated with the parent ID. */ public function testHasManyFormPrePopulated() { $group = $this->objFromFixture('GridFieldDetailFormTest_PeopleGroup', 'group'); $this->logInWithPermission('ADMIN'); $response = $this->get('GridFieldDetailFormTest_Controller'); $parser = new CSSContentParser($response->getBody()); $addLink = $parser->getBySelector('.grid-field .new-link'); $addLink = (string) $addLink[0]['href']; $response = $this->get($addLink); $parser = new CSSContentParser($response->getBody()); $title = $parser->getBySelector('#Form_ItemEditForm_GroupID_Holder span'); $id = $parser->getBySelector('#Form_ItemEditForm_GroupID_Holder input'); $this->assertEquals($group->Name, (string) $title[0]); $this->assertEquals($group->ID, (string) $id[0]['value']); }
public function testSelectWithDisplayFolderName() { $record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); $file4 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file4'); $fileSubfolder = $this->objFromFixture('SilverStripe\\Assets\\File', 'file-subfolder'); $response = $this->get('UploadFieldTest_Controller/Form/field/HasManyDisplayFolder/select/'); $this->assertFalse($response->isError()); // A bit too much coupling with GridField, but a full template overload would make things too complex $parser = new CSSContentParser($response->getBody()); $items = $parser->getBySelector('.ss-gridfield-item'); $itemIDs = array_map(create_function('$el', 'return (int)$el["data-id"];'), $items); $this->assertContains($file4->ID, $itemIDs, 'Contains file in assigned folder'); $this->assertNotContains($fileSubfolder->ID, $itemIDs, 'Does not contain file in subfolder'); }
function testFieldMessageEscapeHtml() { $form = $this->getStubForm(); $form->getController()->handleRequest(new HTTPRequest('GET', '/'), DataModel::inst()); // stub out request $form->addErrorMessage('key1', '<em>Escaped HTML</em>', 'good', true); $form->setupFormErrors(); $parser = new CSSContentParser($result = $form->forTemplate()); $messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message'); $this->assertContains('<em>Escaped HTML</em>', $messageEls[0]->asXML()); $form = $this->getStubForm(); $form->getController()->handleRequest(new HTTPRequest('GET', '/'), DataModel::inst()); // stub out request $form->addErrorMessage('key1', '<em>Unescaped HTML</em>', 'good', false); $form->setupFormErrors(); $parser = new CSSContentParser($form->forTemplate()); $messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message'); $this->assertContains('<em>Unescaped HTML</em>', $messageEls[0]->asXML()); }