8000 [Form] Fixed index checks in PropertyPath classes · symfony/symfony@bbffd1b · GitHub
[go: up one dir, main page]

Skip to content

Commit bbffd1b

Browse files
committed
[Form] Fixed index checks in PropertyPath classes
1 parent ea5ff77 commit bbffd1b

File tree

7 files changed

+318
-0
lines changed

7 files changed

+318
-0
lines changed

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public function getElements()
174174
*/
175175
public function getElement($index)
176176
{
177+
if (!isset($this->elements[$index])) {
178+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the violation path');
179+
}
180+
177181
return $this->elements[$index];
178182
}
179183

@@ -182,6 +186,10 @@ public function getElement($index)
182186
*/
183187
public function isProperty($index)
184188
{
189+
if (!isset($this->isIndex[$index])) {
190+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the violation path');
191+
}
192+
185193
return !$this->isIndex[$index];
186194
}
187195

@@ -190,6 +198,10 @@ public function isProperty($index)
190198
*/
191199
public function isIndex($index)
192200
{
201+
if (!isset($this->isIndex[$index])) {
202+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the violation path');
203+
}
204+
193205
return $this->isIndex[$index];
194206
}
195207

@@ -208,9 +220,15 @@ public function isIndex($index)
208220
* @param integer $index The element index.
209221
*
210222
* @return Boolean Whether the element maps to a form.
223+
*
224+
* @throws \OutOfBoundsException If the offset is invalid.
211225
*/
212226
public function mapsForm($index)
213227
{
228+
if (!isset($this->mapsForm[$index])) {
229+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the violation path');
230+
}
231+
214232
return $this->mapsForm[$index];
215233
}
216234

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationPathTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,117 @@ public function testGetParent($violationPath, $parentPath)
130130

131131
$this->assertEquals($parent, $path->getParent());
132132
}
133+
134+
public function testGetElement()
135+
{
136+
$path = new ViolationPath('children[address].data[street].name');
137+
138+
$this->assertEquals('street', $path->getElement(1));
139+
}
140+
141+
/**
142+
* @expectedException \OutOfBoundsException
143+
*/
144+
public function testGetElementDoesNotAcceptInvalidIndices()
145+
{
146+
$path = new ViolationPath('children[address].data[street].name');
147+
148+
$path->getElement(3);
149+
}
150+
151+
/**
152+
* @expectedException \OutOfBoundsException
153+
*/
154+
public function testGetElementDoesNotAcceptNegativeIndices()
155+
{
156+
$path = new ViolationPath('children[address].data[street].name');
157+
158+
$path->getElement(-1);
159+
}
160+
161+
public function testIsProperty()
162+
{
163+
$path = new ViolationPath('children[address].data[street].name');
164+
165+
$this->assertFalse($path->isProperty(1));
166+
$this->assertTrue($path->isProperty(2));
167+
}
168+
169+
/**
170+
* @expectedException \OutOfBoundsException
171+
*/
172+
public function testIsPropertyDoesNotAcceptInvalidIndices()
173+
{
174+
$path = new ViolationPath('children[address].data[street].name');
175+
176+
$path->isProperty(3);
177+
}
178+
179+
/**
180+
* @expectedException \OutOfBoundsException
181+
*/
182+
public function testIsPropertyDoesNotAcceptNegativeIndices()
183+
{
184+
$path = new ViolationPath('children[address].data[street].name');
185+
186+
$path->isProperty(-1);
187+
}
188+
189+
public function testIsIndex()
190+
{
191+
$path = new ViolationPath('children[address].data[street].name');
192+
193+
$this->assertTrue($path->isIndex(1));
194+
$this->assertFalse($path->isIndex(2));
195+
}
196+
197+
/**
198+
* @expectedException \OutOfBoundsException
199+
*/
200+
public function testIsIndexDoesNotAcceptInvalidIndices()
201+
{
202+
$path = new ViolationPath('children[address].data[street].name');
203+
204+
$path->isIndex(3);
205+
}
206+
207+
/**
208+
* @expectedException \OutOfBoundsException
209+
*/
210+
public function testIsIndexDoesNotAcceptNegativeIndices()
211+
{
212+
$path = new ViolationPath('children[address].data[street].name');
213+
214+
$path->isIndex(-1);
215+
}
216+
217+
public function testMapsForm()
218+
{
219+
$path = new ViolationPath('children[address].data[street].name');
220+
221+
$this->assertTrue($path->mapsForm(0));
222+
$this->assertFalse($path->mapsForm(1));
223+
$this->assertFalse($path->mapsForm(2));
224+
}
225+
226+
/**
227+
* @expectedException \OutOfBoundsException
228+
*/
229+
public function testMapsFormDoesNotAcceptInvalidIndices()
230+
{
231+
$path = new ViolationPath('children[address].data[street].name');
232+
233+
$path->mapsForm(3);
234+
}
235+
236+
/**
237+
* @expectedException \OutOfBoundsException
238+
*/
239+
public function testMapsFormDoesNotAcceptNegativeIndices()
240+
{
241+
$path = new ViolationPath('children[address].data[street].name');
242+
243+
$path->mapsForm(-1);
244+
}
133245
}
246+

src/Symfony/Component/Form/Tests/Util/PropertyPathBuilderTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ public function testReplaceByIndex()
100100
$this->assertEquals($path, $this->builder->getPropertyPath());
101101
}
102102

103+
/**
104+
* @expectedException \OutOfBoundsException
105+
*/
106+
public function testReplaceByIndexDoesNotAllowInvalidOffsets()
107+
{
108+
$this->builder->replaceByIndex(6, 'new1');
109+
}
110+
111+
/**
112+
* @expectedException \OutOfBoundsException
113+
*/
114+
public function testReplaceByIndexDoesNotAllowNegativeOffsets()
115+
{
116+
$this->builder->replaceByIndex(-1, 'new1');
117+
}
118+
103119
public function testReplaceByProperty()
104120
{
105121
$this->builder->replaceByProperty(1, 'new1');
@@ -109,6 +125,22 @@ public function testReplaceByProperty()
109125
$this->assertEquals($path, $this->builder->getPropertyPath());
110126
}
111127

128+
/**
129+
* @expectedException \OutOfBoundsException
130+
*/
131+
public function testReplaceByPropertyDoesNotAllowInvalidOffsets()
132+
{
133+
$this->builder->replaceByProperty(6, 'new1');
134+
}
135+
136+
/**
137+
* @expectedException \OutOfBoundsException
138+
*/
139+
public function testReplaceByPropertyDoesNotAllowNegativeOffsets()
140+
{
141+
$this->builder->replaceByProperty(-1, 'new1');
142+
}
143+
112144
public function testReplace()
113145
{
114146
$this->builder->replace(1, 1, new PropertyPath('new1[new2].new3'));
@@ -118,6 +150,22 @@ public function testReplace()
118150
$this->assertEquals($path, $this->builder->getPropertyPath());
119151
}
120152

153+
/**
154+
* @expectedException \OutOfBoundsException
155+
*/
156+
public function testReplaceDoesNotAllowInvalidOffsets()
157+
{
158+
$this->builder->replace(6, 1, new PropertyPath('new1[new2].new3'));
159+
}
160+
161+
/**
162+
* @expectedException \OutOfBoundsException
163+
*/
164+
public function testReplaceDoesNotAllowNegativeOffsets()
165+
{
166+
$this->builder->replace(-1, 1, new PropertyPath('new1[new2].new3'));
167+
}
168+
121169
public function testReplaceWithLengthGreaterOne()
122170
{
123171
$this->builder->replace(0, 2, new PropertyPath('new1[new2].new3'));
@@ -153,4 +201,20 @@ public function testRemove()
153201

154202
$this->assertEquals($path, $this->builder->getPropertyPath());
155203
}
204+
205+
/**
206+
* @expectedException \OutOfBoundsException
207+
*/
208+
public function testRemoveDoesNotAllowInvalidOffsets()
209+
{
210+
$this->builder->remove(6);
211+
}
212+
213+
/**
214+
* @expectedException \OutOfBoundsException
215+
*/
216+
public function testRemoveDoesNotAllowNegativeOffsets()
217+
{
218+
$this->builder->remove(-1);
219+
}
156220
}

src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,87 @@ public function testCopyConstructor()
497497

498498
$this->assertEquals($propertyPath, $copy);
499499
}
500+
501+
public function testGetElement()
502+
{
503+
$propertyPath = new PropertyPath('grandpa.parent[child]');
504+
505+
$this->assertEquals('child', $propertyPath->getElement(2));
506+
}
507+
508+
/**
509+
* @expectedException \OutOfBoundsException
510+
*/
511+
public function testGetElementDoesNotAcceptInvalidIndices()
512+
{
513+
$propertyPath = new PropertyPath('grandpa.parent[child]');
514+
515+
$propertyPath->getElement(3);
516+
}
517+
518+
/**
519+
* @expectedException \OutOfBoundsException
520+
*/
521+
public function testGetElementDoesNotAcceptNegativeIndices()
522+
{
523+
$propertyPath = new PropertyPath('grandpa.parent[child]');
524+
525+
$propertyPath->getElement(-1);
526+
}
527+
528+
public function testIsProperty()
529+
{
530+
$propertyPath = new PropertyPath('grandpa.parent[child]');
531+
532+
$this->assertTrue($propertyPath->isProperty(1));
533+
$this->assertFalse($propertyPath->isProperty(2));
534+
}
535+
536+
/**
537+
* @expectedException \OutOfBoundsException
538+
*/
539+
public function testIsPropertyDoesNotAcceptInvalidIndices()
540+
{
541+
$propertyPath = new PropertyPath('grandpa.parent[child]');
542+
543+
$propertyPath->isProperty(3);
544+
}
545+
546+
/**
547+
* @expectedException \OutOfBoundsException
548+
*/
549+
public function testIsPropertyDoesNotAcceptNegativeIndices()
550+
{
551+
$propertyPath = new PropertyPath('grandpa.parent[child]');
552+
553+
$propertyPath->isProperty(-1);
554+
}
555+
556+
public function testIsIndex()
557+
{
558+
$propertyPath = new PropertyPath('grandpa.parent[child]');
559+
560+
$this->assertFalse($propertyPath->isIndex(1));
561+
$this->assertTrue($propertyPath->isIndex(2));
562+
}
563+
564+
/**
565+
* @expectedException \OutOfBoundsException
566+
*/
567+
public function testIsIndexDoesNotAcceptInvalidIndices()
568+
{
569+
$propertyPath = new PropertyPath('grandpa.parent[child]');
570+
571+
$propertyPath->isIndex(3);
572+
}
573+
574+
/**
575+
* @expectedException \OutOfBoundsException
576+
*/
577+
public function testIsIndexDoesNotAcceptNegativeIndices()
578+
{
579+
$propertyPath = new PropertyPath('grandpa.parent[child]');
580+
581+
$propertyPath->isIndex(-1);
582+
}
500583
}

src/Symfony/Component/Form/Util/PropertyPath.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ public function getElements()
212212
*/
213213
public function getElement($index)
214214
{
215+
if (!isset($this->elements[$index])) {
216+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the property path');
217+
}
218+
215219
return $this->elements[$index];
216220
}
217221

@@ -220,6 +224,10 @@ public function getElement($index)
220224
*/
221225
public function isProperty($index)
222226
{
227+
if (!isset($this->isIndex[$index])) {
228+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the property path');
229+
}
230+
223231
return !$this->isIndex[$index];
224232
}
225233

@@ -228,6 +236,10 @@ public function isProperty($index)
228236
*/
229237
public function isIndex($index)
230238
{
239+
if (!isset($this->isIndex[$index])) {
240+
throw new \OutOfBoundsException('The index ' . $index . ' is not within the property path');
241+
}
242+
231243
return $this->isIndex[$index];
232244
}
233245

0 commit comments

Comments
 (0)
0