From cb86fdb3d1bae4d55848b5b3e6f439160752b0a4 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 13 Aug 2008 01:39:46 +0000 Subject: [PATCH] BUGFIX: Fixed bug with unpaginated TableListFields. Added tests for TableListField pagination git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60573 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/TableListField.php | 2 +- tests/forms/TableListFieldTest.php | 60 ++++++++++++++++++++++++++++-- tests/forms/TableListFieldTest.yml | 14 ++++++- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/forms/TableListField.php b/forms/TableListField.php index a40a4f456..bc29b6917 100755 --- a/forms/TableListField.php +++ b/forms/TableListField.php @@ -361,7 +361,7 @@ JS } function sourceItems() { - $SQL_limit = ($this->showPagination && $this->pageSize) ? "{$this->pageSize}" : "0"; + $SQL_limit = ($this->showPagination && $this->pageSize) ? "{$this->pageSize}" : null; if(isset($_REQUEST['ctf'][$this->Name()]['start']) && is_numeric($_REQUEST['ctf'][$this->Name()]['start'])) { $SQL_start = (isset($_REQUEST['ctf'][$this->Name()]['start'])) ? intval($_REQUEST['ctf'][$this->Name()]['start']) : "0"; } else { diff --git a/tests/forms/TableListFieldTest.php b/tests/forms/TableListFieldTest.php index 529e19222..20469e149 100644 --- a/tests/forms/TableListFieldTest.php +++ b/tests/forms/TableListFieldTest.php @@ -3,7 +3,7 @@ class TableListFieldTest extends SapphireTest { static $fixture_file = 'sapphire/tests/forms/TableListFieldTest.yml'; - function testCanReferenceCustomMethodsAndFiledsOnObject() { + function testCanReferenceCustomMethodsAndFieldsOnObject() { $table = new TableListField("Tester", "TableListFieldTest_Obj", array( "A" => "Col A", "B" => "Col B", @@ -11,14 +11,68 @@ class TableListFieldTest extends SapphireTest { "D" => "Col D", "E" => "Col E", )); - $result = $table->FieldHolder(); - + // Do a quick check to ensure that some of the D() and getE() values got through $this->assertRegExp('/>\s*a2\s*assertRegExp('/>\s*a2\/b2\/c2\s*assertRegExp('/>\s*a2-e "Col A", + "B" => "Col B", + "C" => "Col C", + "D" => "Col D", + "E" => "Col E", + )); + $items = $table->sourceItems(); + $this->assertNotNull($items); + + $itemMap = $items->toDropdownMap("ID", "A") ; + $this->assertEquals(array(1 => "a1", 2 => "a2", 3 => "a3", 4 => "a4", 5 => "a5"), $itemMap); + } + + function testFirstPageOfPaginatedSourceItemGeneration() { + /* With pagination enabled, only the first page of items should be shown */ + $table = new TableListField("Tester", "TableListFieldTest_Obj", array( + "A" => "Col A", + "B" => "Col B", + "C" => "Col C", + "D" => "Col D", + "E" => "Col E", + )); + $table->ShowPagination = true; + $table->PageSize = 2; + + $items = $table->sourceItems(); + $this->assertNotNull($items); + + $itemMap = $items->toDropdownMap("ID", "A") ; + $this->assertEquals(array(1 => "a1", 2 => "a2"), $itemMap); + } + + function testSecondPageOfPaginatedSourceItemGeneration() { + /* With pagination enabled, only the first page of items should be shown */ + $table = new TableListField("Tester", "TableListFieldTest_Obj", array( + "A" => "Col A", + "B" => "Col B", + "C" => "Col C", + "D" => "Col D", + "E" => "Col E", + )); + $table->ShowPagination = true; + $table->PageSize = 2; + $_REQUEST['ctf']['Tester']['start'] = 2; + + $items = $table->sourceItems(); + $this->assertNotNull($items); + + $itemMap = $items->toDropdownMap("ID", "A") ; + $this->assertEquals(array(3 => "a3", 4 => "a4"), $itemMap); + } } class TableListFieldTest_Obj extends DataObject implements TestOnly { diff --git a/tests/forms/TableListFieldTest.yml b/tests/forms/TableListFieldTest.yml index cba464584..dc46ee327 100644 --- a/tests/forms/TableListFieldTest.yml +++ b/tests/forms/TableListFieldTest.yml @@ -7,4 +7,16 @@ TableListFieldTest_Obj: A: a2 B: b2 C: c2 - \ No newline at end of file + three: + A: a3 + B: b3 + C: c3 + four: + A: a4 + B: b4 + C: c4 + five: + A: a5 + B: b5 + C: c5 + \ No newline at end of file