FormTest.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form;
  11. require_once __DIR__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. require_once __DIR__ . '/Fixtures/TestForm.php';
  14. use Symfony\Component\Form\Form;
  15. use Symfony\Component\Form\FormContext;
  16. use Symfony\Component\Form\Field;
  17. use Symfony\Component\Form\FieldError;
  18. use Symfony\Component\Form\DataError;
  19. use Symfony\Component\Form\HiddenField;
  20. use Symfony\Component\Form\PropertyPath;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\File\UploadedFile;
  23. use Symfony\Component\Validator\ConstraintViolation;
  24. use Symfony\Component\Validator\ConstraintViolationList;
  25. use Symfony\Component\Validator\ExecutionContext;
  26. use Symfony\Tests\Component\Form\Fixtures\Author;
  27. use Symfony\Tests\Component\Form\Fixtures\TestField;
  28. use Symfony\Tests\Component\Form\Fixtures\TestForm;
  29. class FormTest_PreconfiguredForm extends Form
  30. {
  31. protected function configure()
  32. {
  33. $this->add(new Field('firstName'));
  34. parent::configure();
  35. }
  36. }
  37. // behaves like a form with a value transformer that transforms into
  38. // a specific format
  39. class FormTest_FormThatReturns extends Form
  40. {
  41. protected $returnValue;
  42. public function setReturnValue($returnValue)
  43. {
  44. $this->returnValue = $returnValue;
  45. }
  46. public function setData($data)
  47. {
  48. }
  49. public function getData()
  50. {
  51. return $this->returnValue;
  52. }
  53. }
  54. class FormTest_AuthorWithoutRefSetter
  55. {
  56. protected $reference;
  57. protected $referenceCopy;
  58. public function __construct($reference)
  59. {
  60. $this->reference = $reference;
  61. $this->referenceCopy = $reference;
  62. }
  63. // The returned object should be modified by reference without having
  64. // to provide a setReference() method
  65. public function getReference()
  66. {
  67. return $this->reference;
  68. }
  69. // The returned object is a copy, so setReferenceCopy() must be used
  70. // to update it
  71. public function getReferenceCopy()
  72. {
  73. return is_object($this->referenceCopy) ? clone $this->referenceCopy : $this->referenceCopy;
  74. }
  75. public function setReferenceCopy($reference)
  76. {
  77. $this->referenceCopy = $reference;
  78. }
  79. }
  80. class TestSetDataBeforeConfigureForm extends Form
  81. {
  82. protected $testCase;
  83. protected $object;
  84. public function __construct($testCase, $name, $object, $validator)
  85. {
  86. $this->testCase = $testCase;
  87. $this->object = $object;
  88. parent::__construct($name, $object, $validator);
  89. }
  90. protected function configure()
  91. {
  92. $this->testCase->assertEquals($this->object, $this->getData());
  93. parent::configure();
  94. }
  95. }
  96. class FormTest extends \PHPUnit_Framework_TestCase
  97. {
  98. protected $validator;
  99. protected $form;
  100. public static function setUpBeforeClass()
  101. {
  102. @session_start();
  103. }
  104. protected function setUp()
  105. {
  106. $this->validator = $this->createMockValidator();
  107. $this->form = new Form('author', array('validator' => $this->validator));
  108. }
  109. public function testNoCsrfProtectionByDefault()
  110. {
  111. $form = new Form('author');
  112. $this->assertFalse($form->isCsrfProtected());
  113. }
  114. public function testCsrfProtectionCanBeEnabled()
  115. {
  116. $form = new Form('author', array(
  117. 'csrf_provider' => $this->createMockCsrfProvider(),
  118. ));
  119. $this->assertTrue($form->isCsrfProtected());
  120. }
  121. public function testCsrfFieldNameCanBeSet()
  122. {
  123. $form = new Form('author', array(
  124. 'csrf_provider' => $this->createMockCsrfProvider(),
  125. 'csrf_field_name' => 'foobar',
  126. ));
  127. $this->assertEquals('foobar', $form->getCsrfFieldName());
  128. }
  129. public function testCsrfProtectedFormsHaveExtraField()
  130. {
  131. $provider = $this->createMockCsrfProvider();
  132. $provider->expects($this->once())
  133. ->method('generateCsrfToken')
  134. ->with($this->equalTo('Symfony\Component\Form\Form'))
  135. ->will($this->returnValue('ABCDEF'));
  136. $form = new Form('author', array(
  137. 'csrf_provider' => $provider,
  138. ));
  139. $this->assertTrue($form->has($this->form->getCsrfFieldName()));
  140. $field = $form->get($form->getCsrfFieldName());
  141. $this->assertTrue($field instanceof HiddenField);
  142. $this->assertEquals('ABCDEF', $field->getDisplayedData());
  143. }
  144. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  145. {
  146. $this->form->submit(array());
  147. $this->assertTrue($this->form->isCsrfTokenValid());
  148. }
  149. public function testIsCsrfTokenValidPasses()
  150. {
  151. $provider = $this->createMockCsrfProvider();
  152. $provider->expects($this->once())
  153. ->method('isCsrfTokenValid')
  154. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  155. ->will($this->returnValue(true));
  156. $form = new Form('author', array(
  157. 'csrf_provider' => $provider,
  158. 'validator' => $this->validator,
  159. ));
  160. $field = $form->getCsrfFieldName();
  161. $form->submit(array($field => 'ABCDEF'));
  162. $this->assertTrue($form->isCsrfTokenValid());
  163. }
  164. public function testIsCsrfTokenValidFails()
  165. {
  166. $provider = $this->createMockCsrfProvider();
  167. $provider->expects($this->once())
  168. ->method('isCsrfTokenValid')
  169. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  170. ->will($this->returnValue(false));
  171. $form = new Form('author', array(
  172. 'csrf_provider' => $provider,
  173. 'validator' => $this->validator,
  174. ));
  175. $field = $form->getCsrfFieldName();
  176. $form->submit(array($field => 'ABCDEF'));
  177. $this->assertFalse($form->isCsrfTokenValid());
  178. }
  179. public function testGetValidator()
  180. {
  181. $this->assertSame($this->validator, $this->form->getValidator());
  182. }
  183. public function testValidationGroupNullByDefault()
  184. {
  185. $this->assertNull($this->form->getValidationGroups());
  186. }
  187. public function testValidationGroupsCanBeSetToString()
  188. {
  189. $form = new Form('author', array(
  190. 'validation_groups' => 'group',
  191. ));
  192. $this->assertEquals(array('group'), $form->getValidationGroups());
  193. }
  194. public function testValidationGroupsCanBeSetToArray()
  195. {
  196. $form = new Form('author', array(
  197. 'validation_groups' => array('group1', 'group2'),
  198. ));
  199. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  200. }
  201. public function testValidationGroupsAreInheritedFromParentIfEmpty()
  202. {
  203. $parentForm = new Form('parent', array(
  204. 'validation_groups' => 'group',
  205. ));
  206. $childForm = new Form('child');
  207. $parentForm->add($childForm);
  208. $this->assertEquals(array('group'), $childForm->getValidationGroups());
  209. }
  210. public function testValidationGroupsAreNotInheritedFromParentIfSet()
  211. {
  212. $parentForm = new Form('parent', array(
  213. 'validation_groups' => 'group1',
  214. ));
  215. $childForm = new Form('child', array(
  216. 'validation_groups' => 'group2',
  217. ));
  218. $parentForm->add($childForm);
  219. $this->assertEquals(array('group2'), $childForm->getValidationGroups());
  220. }
  221. /**
  222. * @expectedException Symfony\Component\Form\Exception\FormException
  223. */
  224. public function testBindThrowsExceptionIfAnonymous()
  225. {
  226. $form = new Form(null, array('validator' => $this->createMockValidator()));
  227. $form->bind($this->createPostRequest());
  228. }
  229. public function testBindValidatesData()
  230. {
  231. $form = new Form('author', array(
  232. 'validation_groups' => 'group',
  233. 'validator' => $this->validator,
  234. ));
  235. $form->add(new TestField('firstName'));
  236. $this->validator->expects($this->once())
  237. ->method('validate')
  238. ->with($this->equalTo($form));
  239. // concrete request is irrelevant
  240. $form->bind($this->createPostRequest());
  241. }
  242. public function testBindDoesNotValidateArrays()
  243. {
  244. $form = new Form('author', array(
  245. 'validator' => $this->validator,
  246. ));
  247. $form->add(new TestField('firstName'));
  248. // only the form is validated
  249. $this->validator->expects($this->once())
  250. ->method('validate')
  251. ->with($this->equalTo($form));
  252. // concrete request is irrelevant
  253. // data is an array
  254. $form->bind($this->createPostRequest(), array());
  255. }
  256. public function testBindThrowsExceptionIfNoValidatorIsSet()
  257. {
  258. $field = $this->createMockField('firstName');
  259. $form = new Form('author');
  260. $form->add($field);
  261. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  262. // data is irrelevant
  263. $form->bind($this->createPostRequest());
  264. }
  265. public function testBindReadsRequestData()
  266. {
  267. $values = array(
  268. 'author' => array(
  269. 'name' => 'Bernhard',
  270. 'image' => array('filename' => 'foobar.png'),
  271. ),
  272. );
  273. $files = array(
  274. 'author' => array(
  275. 'error' => array('image' => array('file' => UPLOAD_ERR_OK)),
  276. 'name' => array('image' => array('file' => 'upload.png')),
  277. 'size' => array('image' => array('file' => 123)),
  278. 'tmp_name' => array('image' => array('file' => 'abcdef.png')),
  279. 'type' => array('image' => array('file' => 'image/png')),
  280. ),
  281. );
  282. $form = new Form('author', array('validator' => $this->validator));
  283. $form->add(new TestField('name'));
  284. $imageForm = new Form('image');
  285. $imageForm->add(new TestField('file'));
  286. $imageForm->add(new TestField('filename'));
  287. $form->add($imageForm);
  288. $form->bind($this->createPostRequest($values, $files));
  289. $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  290. $this->assertEquals('Bernhard', $form['name']->getData());
  291. $this->assertEquals('foobar.png', $form['image']['filename']->getData());
  292. $this->assertEquals($file, $form['image']['file']->getData());
  293. }
  294. public function testBindAcceptsObject()
  295. {
  296. $object = new \stdClass();
  297. $form = new Form('author', array('validator' => $this->validator));
  298. $form->bind(new Request(), $object);
  299. $this->assertSame($object, $form->getData());
  300. }
  301. public function testReadPropertyIsIgnoredIfPropertyPathIsNull()
  302. {
  303. $author = new Author();
  304. $author->child = new Author();
  305. $standaloneChild = new Author();
  306. $form = new Form('child');
  307. $form->setData($standaloneChild);
  308. $form->setPropertyPath(null);
  309. $form->readProperty($author);
  310. // should not be $author->child!!
  311. $this->assertSame($standaloneChild, $form->getData());
  312. }
  313. public function testWritePropertyIsIgnoredIfPropertyPathIsNull()
  314. {
  315. $author = new Author();
  316. $author->child = $child = new Author();
  317. $standaloneChild = new Author();
  318. $form = new Form('child');
  319. $form->setData($standaloneChild);
  320. $form->setPropertyPath(null);
  321. $form->writeProperty($author);
  322. // $author->child was not modified
  323. $this->assertSame($child, $author->child);
  324. }
  325. public function testSupportsArrayAccess()
  326. {
  327. $form = new Form('author');
  328. $form->add($this->createMockField('firstName'));
  329. $this->assertEquals($form->get('firstName'), $form['firstName']);
  330. $this->assertTrue(isset($form['firstName']));
  331. }
  332. public function testSupportsUnset()
  333. {
  334. $form = new Form('author');
  335. $form->add($this->createMockField('firstName'));
  336. unset($form['firstName']);
  337. $this->assertFalse(isset($form['firstName']));
  338. }
  339. public function testDoesNotSupportAddingFields()
  340. {
  341. $form = new Form('author');
  342. $this->setExpectedException('LogicException');
  343. $form[] = $this->createMockField('lastName');
  344. }
  345. public function testSupportsCountable()
  346. {
  347. $form = new Form('group');
  348. $form->add($this->createMockField('firstName'));
  349. $form->add($this->createMockField('lastName'));
  350. $this->assertEquals(2, count($form));
  351. $form->add($this->createMockField('australian'));
  352. $this->assertEquals(3, count($form));
  353. }
  354. public function testSupportsIterable()
  355. {
  356. $form = new Form('group');
  357. $form->add($field1 = $this->createMockField('field1'));
  358. $form->add($field2 = $this->createMockField('field2'));
  359. $form->add($field3 = $this->createMockField('field3'));
  360. $expected = array(
  361. 'field1' => $field1,
  362. 'field2' => $field2,
  363. 'field3' => $field3,
  364. );
  365. $this->assertEquals($expected, iterator_to_array($form));
  366. }
  367. public function testIsSubmitted()
  368. {
  369. $form = new Form('author', array('validator' => $this->validator));
  370. $this->assertFalse($form->isSubmitted());
  371. $form->submit(array('firstName' => 'Bernhard'));
  372. $this->assertTrue($form->isSubmitted());
  373. }
  374. public function testValidIfAllFieldsAreValid()
  375. {
  376. $form = new Form('author', array('validator' => $this->validator));
  377. $form->add($this->createValidMockField('firstName'));
  378. $form->add($this->createValidMockField('lastName'));
  379. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  380. $this->assertTrue($form->isValid());
  381. }
  382. public function testInvalidIfFieldIsInvalid()
  383. {
  384. $form = new Form('author', array('validator' => $this->validator));
  385. $form->add($this->createInvalidMockField('firstName'));
  386. $form->add($this->createValidMockField('lastName'));
  387. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  388. $this->assertFalse($form->isValid());
  389. }
  390. public function testInvalidIfSubmittedWithExtraFields()
  391. {
  392. $form = new Form('author', array('validator' => $this->validator));
  393. $form->add($this->createValidMockField('firstName'));
  394. $form->add($this->createValidMockField('lastName'));
  395. $form->submit(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  396. $this->assertTrue($form->isSubmittedWithExtraFields());
  397. }
  398. public function testHasNoErrorsIfOnlyFieldHasErrors()
  399. {
  400. $form = new Form('author', array('validator' => $this->validator));
  401. $form->add($this->createInvalidMockField('firstName'));
  402. $form->submit(array('firstName' => 'Bernhard'));
  403. $this->assertFalse($form->hasErrors());
  404. }
  405. public function testSubmitForwardsPreprocessedData()
  406. {
  407. $field = $this->createMockField('firstName');
  408. $form = $this->getMock(
  409. 'Symfony\Component\Form\Form',
  410. array('preprocessData'), // only mock preprocessData()
  411. array('author', array('validator' => $this->validator))
  412. );
  413. // The data array is prepared directly after binding
  414. $form->expects($this->once())
  415. ->method('preprocessData')
  416. ->with($this->equalTo(array('firstName' => 'Bernhard')))
  417. ->will($this->returnValue(array('firstName' => 'preprocessed[Bernhard]')));
  418. $form->add($field);
  419. // The preprocessed data is then forwarded to the fields
  420. $field->expects($this->once())
  421. ->method('submit')
  422. ->with($this->equalTo('preprocessed[Bernhard]'));
  423. $form->submit(array('firstName' => 'Bernhard'));
  424. }
  425. public function testSubmitForwardsNullIfValueIsMissing()
  426. {
  427. $field = $this->createMockField('firstName');
  428. $field->expects($this->once())
  429. ->method('submit')
  430. ->with($this->equalTo(null));
  431. $form = new Form('author', array('validator' => $this->validator));
  432. $form->add($field);
  433. $form->submit(array());
  434. }
  435. public function testAddErrorMapsFieldValidationErrorsOntoFields()
  436. {
  437. $error = new FieldError('Message');
  438. $field = $this->createMockField('firstName');
  439. $field->expects($this->once())
  440. ->method('addError')
  441. ->with($this->equalTo($error));
  442. $form = new Form('author');
  443. $form->add($field);
  444. $path = new PropertyPath('fields[firstName].data');
  445. $form->addError(new FieldError('Message'), $path->getIterator());
  446. }
  447. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedForms()
  448. {
  449. $error = new FieldError('Message');
  450. $field = $this->createMockField('firstName');
  451. $field->expects($this->once())
  452. ->method('addError')
  453. ->with($this->equalTo($error));
  454. $form = new Form('author');
  455. $innerGroup = new Form('names');
  456. $innerGroup->add($field);
  457. $form->add($innerGroup);
  458. $path = new PropertyPath('fields[names].fields[firstName].data');
  459. $form->addError(new FieldError('Message'), $path->getIterator());
  460. }
  461. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  462. {
  463. $field = $this->createMockField('foo');
  464. $field->expects($this->never())
  465. ->method('addError');
  466. $form = new Form('author');
  467. $form->add($field);
  468. $path = new PropertyPath('fields[bar].data');
  469. $form->addError(new FieldError('Message'), $path->getIterator());
  470. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  471. }
  472. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  473. {
  474. $field = $this->createMockField('firstName');
  475. $field->expects($this->any())
  476. ->method('isHidden')
  477. ->will($this->returnValue(true));
  478. $field->expects($this->never())
  479. ->method('addError');
  480. $form = new Form('author');
  481. $form->add($field);
  482. $path = new PropertyPath('fields[firstName].data');
  483. $form->addError(new FieldError('Message'), $path->getIterator());
  484. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  485. }
  486. public function testAddErrorMapsDataValidationErrorsOntoFields()
  487. {
  488. $error = new DataError('Message');
  489. // path is expected to point at "firstName"
  490. $expectedPath = new PropertyPath('firstName');
  491. $expectedPathIterator = $expectedPath->getIterator();
  492. $field = $this->createMockField('firstName');
  493. $field->expects($this->any())
  494. ->method('getPropertyPath')
  495. ->will($this->returnValue(new PropertyPath('firstName')));
  496. $field->expects($this->once())
  497. ->method('addError')
  498. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  499. $form = new Form('author');
  500. $form->add($field);
  501. $path = new PropertyPath('firstName');
  502. $form->addError($error, $path->getIterator());
  503. }
  504. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  505. {
  506. $field = $this->createMockField('foo');
  507. $field->expects($this->any())
  508. ->method('getPropertyPath')
  509. ->will($this->returnValue(new PropertyPath('foo')));
  510. $field->expects($this->never())
  511. ->method('addError');
  512. $form = new Form('author');
  513. $form->add($field);
  514. $path = new PropertyPath('bar');
  515. $form->addError(new DataError('Message'), $path->getIterator());
  516. }
  517. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  518. {
  519. $field = $this->createMockField('firstName');
  520. $field->expects($this->any())
  521. ->method('isHidden')
  522. ->will($this->returnValue(true));
  523. $field->expects($this->any())
  524. ->method('getPropertyPath')
  525. ->will($this->returnValue(new PropertyPath('firstName')));
  526. $field->expects($this->never())
  527. ->method('addError');
  528. $form = new Form('author');
  529. $form->add($field);
  530. $path = new PropertyPath('firstName');
  531. $form->addError(new DataError('Message'), $path->getIterator());
  532. }
  533. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  534. {
  535. $error = new DataError('Message');
  536. // path is expected to point at "street"
  537. $expectedPath = new PropertyPath('address.street');
  538. $expectedPathIterator = $expectedPath->getIterator();
  539. $expectedPathIterator->next();
  540. $field = $this->createMockField('address');
  541. $field->expects($this->any())
  542. ->method('getPropertyPath')
  543. ->will($this->returnValue(new PropertyPath('address')));
  544. $field->expects($this->once())
  545. ->method('addError')
  546. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  547. $form = new Form('author');
  548. $form->add($field);
  549. $path = new PropertyPath('address.street');
  550. $form->addError($error, $path->getIterator());
  551. }
  552. public function testAddErrorMapsErrorsOntoFieldsInVirtualGroups()
  553. {
  554. $error = new DataError('Message');
  555. // path is expected to point at "address"
  556. $expectedPath = new PropertyPath('address');
  557. $expectedPathIterator = $expectedPath->getIterator();
  558. $field = $this->createMockField('address');
  559. $field->expects($this->any())
  560. ->method('getPropertyPath')
  561. ->will($this->returnValue(new PropertyPath('address')));
  562. $field->expects($this->once())
  563. ->method('addError')
  564. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  565. $form = new Form('author');
  566. $nestedForm = new Form('nested', array('virtual' => true));
  567. $nestedForm->add($field);
  568. $form->add($nestedForm);
  569. $path = new PropertyPath('address');
  570. $form->addError($error, $path->getIterator());
  571. }
  572. public function testAddThrowsExceptionIfAlreadySubmitted()
  573. {
  574. $form = new Form('author', array('validator' => $this->validator));
  575. $form->add($this->createMockField('firstName'));
  576. $form->submit(array());
  577. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadySubmittedException');
  578. $form->add($this->createMockField('lastName'));
  579. }
  580. public function testAddSetsFieldParent()
  581. {
  582. $form = new Form('author');
  583. $field = $this->createMockField('firstName');
  584. $field->expects($this->once())
  585. ->method('setParent')
  586. ->with($this->equalTo($form));
  587. $form->add($field);
  588. }
  589. public function testRemoveUnsetsFieldParent()
  590. {
  591. $form = new Form('author');
  592. $field = $this->createMockField('firstName');
  593. $field->expects($this->exactly(2))
  594. ->method('setParent');
  595. // PHPUnit fails to compare subsequent method calls with different arguments
  596. $form->add($field);
  597. $form->remove('firstName');
  598. }
  599. public function testAddUpdatesFieldFromTransformedData()
  600. {
  601. $originalAuthor = new Author();
  602. $transformedAuthor = new Author();
  603. // the authors should differ to make sure the test works
  604. $transformedAuthor->firstName = 'Foo';
  605. $form = new TestForm('author');
  606. $transformer = $this->createMockTransformer();
  607. $transformer->expects($this->once())
  608. ->method('transform')
  609. ->with($this->equalTo($originalAuthor))
  610. ->will($this->returnValue($transformedAuthor));
  611. $form->setValueTransformer($transformer);
  612. $form->setData($originalAuthor);
  613. $field = $this->createMockField('firstName');
  614. $field->expects($this->any())
  615. ->method('getPropertyPath')
  616. ->will($this->returnValue(new PropertyPath('firstName')));
  617. $field->expects($this->once())
  618. ->method('readProperty')
  619. ->with($this->equalTo($transformedAuthor));
  620. $form->add($field);
  621. }
  622. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  623. {
  624. $originalAuthor = new Author();
  625. $form = new TestForm('author');
  626. $transformer = $this->createMockTransformer();
  627. $transformer->expects($this->once())
  628. ->method('transform')
  629. ->with($this->equalTo($originalAuthor))
  630. ->will($this->returnValue(''));
  631. $form->setValueTransformer($transformer);
  632. $form->setData($originalAuthor);
  633. $field = $this->createMockField('firstName');
  634. $field->expects($this->never())
  635. ->method('readProperty');
  636. $form->add($field);
  637. }
  638. /**
  639. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  640. */
  641. public function testAddThrowsExceptionIfNoFieldOrString()
  642. {
  643. $form = new Form('author');
  644. $form->add(1234);
  645. }
  646. /**
  647. * @expectedException Symfony\Component\Form\Exception\FieldDefinitionException
  648. */
  649. public function testAddThrowsExceptionIfAnonymousField()
  650. {
  651. $form = new Form('author');
  652. $field = $this->createMockField('');
  653. $form->add($field);
  654. }
  655. /**
  656. * @expectedException Symfony\Component\Form\Exception\FormException
  657. */
  658. public function testAddThrowsExceptionIfStringButNoFieldFactory()
  659. {
  660. $form = new Form('author', array('data_class' => 'Application\Entity'));
  661. $form->add('firstName');
  662. }
  663. /**
  664. * @expectedException Symfony\Component\Form\Exception\FormException
  665. */
  666. public function testAddThrowsExceptionIfStringButNoClass()
  667. {
  668. $form = new Form('author', array('field_factory' => new \stdClass()));
  669. $form->add('firstName');
  670. }
  671. public function testAddUsesFieldFromFactoryIfStringIsGiven()
  672. {
  673. $author = new \stdClass();
  674. $field = $this->createMockField('firstName');
  675. $factory = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryInterface');
  676. $factory->expects($this->once())
  677. ->method('getInstance')
  678. ->with($this->equalTo('stdClass'), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar')))
  679. ->will($this->returnValue($field));
  680. $form = new Form('author', array(
  681. 'data' => $author,
  682. 'data_class' => 'stdClass',
  683. 'field_factory' => $factory,
  684. ));
  685. $form->add('firstName', array('foo' => 'bar'));
  686. $this->assertSame($field, $form['firstName']);
  687. }
  688. public function testSetDataUpdatesAllFieldsFromTransformedData()
  689. {
  690. $originalAuthor = new Author();
  691. $transformedAuthor = new Author();
  692. // the authors should differ to make sure the test works
  693. $transformedAuthor->firstName = 'Foo';
  694. $form = new TestForm('author');
  695. $transformer = $this->createMockTransformer();
  696. $transformer->expects($this->once())
  697. ->method('transform')
  698. ->with($this->equalTo($originalAuthor))
  699. ->will($this->returnValue($transformedAuthor));
  700. $form->setValueTransformer($transformer);
  701. $field = $this->createMockField('firstName');
  702. $field->expects($this->once())
  703. ->method('readProperty')
  704. ->with($this->equalTo($transformedAuthor));
  705. $form->add($field);
  706. $field = $this->createMockField('lastName');
  707. $field->expects($this->once())
  708. ->method('readProperty')
  709. ->with($this->equalTo($transformedAuthor));
  710. $form->add($field);
  711. $form->setData($originalAuthor);
  712. }
  713. /**
  714. * The use case for this test are groups whose fields should be mapped
  715. * directly onto properties of the form's object.
  716. *
  717. * Example:
  718. *
  719. * <code>
  720. * $dateRangeField = new Form('dateRange');
  721. * $dateRangeField->add(new DateField('startDate'));
  722. * $dateRangeField->add(new DateField('endDate'));
  723. * $form->add($dateRangeField);
  724. * </code>
  725. *
  726. * If $dateRangeField is not virtual, the property "dateRange" must be
  727. * present on the form's object. In this property, an object or array
  728. * with the properties "startDate" and "endDate" is expected.
  729. *
  730. * If $dateRangeField is virtual though, it's children are mapped directly
  731. * onto the properties "startDate" and "endDate" of the form's object.
  732. */
  733. public function testSetDataSkipsVirtualForms()
  734. {
  735. $author = new Author();
  736. $author->firstName = 'Foo';
  737. $form = new Form('author');
  738. $nestedForm = new Form('personal_data', array(
  739. 'virtual' => true,
  740. ));
  741. // both fields are in the nested group but receive the object of the
  742. // top-level group because the nested group is virtual
  743. $field = $this->createMockField('firstName');
  744. $field->expects($this->once())
  745. ->method('readProperty')
  746. ->with($this->equalTo($author));
  747. $nestedForm->add($field);
  748. $field = $this->createMockField('lastName');
  749. $field->expects($this->once())
  750. ->method('readProperty')
  751. ->with($this->equalTo($author));
  752. $nestedForm->add($field);
  753. $form->add($nestedForm);
  754. $form->setData($author);
  755. }
  756. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  757. {
  758. $form = new Form('author');
  759. $this->setExpectedException('InvalidArgumentException');
  760. $form->setData('foobar');
  761. }
  762. /**
  763. * @expectedException Symfony\Component\Form\Exception\FormException
  764. */
  765. public function testSetDataMatchesAgainstDataClass_fails()
  766. {
  767. $form = new Form('author', array(
  768. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  769. ));
  770. $form->setData(new \stdClass());
  771. }
  772. public function testSetDataMatchesAgainstDataClass_succeeds()
  773. {
  774. $form = new Form('author', array(
  775. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  776. ));
  777. $form->setData(new Author());
  778. }
  779. public function testSetDataToNull()
  780. {
  781. $form = new Form('author');
  782. $form->setData(null);
  783. $this->assertNull($form->getData());
  784. }
  785. public function testSetDataToNullCreatesObjectIfClassAvailable()
  786. {
  787. $form = new Form('author', array(
  788. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  789. ));
  790. $form->setData(null);
  791. $this->assertEquals(new Author(), $form->getData());
  792. }
  793. public function testSetDataToNullUsesDataConstructorOption()
  794. {
  795. $author = new Author();
  796. $form = new Form('author', array(
  797. 'data_constructor' => function () use ($author) {
  798. return $author;
  799. }
  800. ));
  801. $form->setData(null);
  802. $this->assertSame($author, $form->getData());
  803. }
  804. public function testSubmitUpdatesTransformedDataFromAllFields()
  805. {
  806. $originalAuthor = new Author();
  807. $transformedAuthor = new Author();
  808. // the authors should differ to make sure the test works
  809. $transformedAuthor->firstName = 'Foo';
  810. $form = new TestForm('author', array('validator' => $this->validator));
  811. $transformer = $this->createMockTransformer();
  812. $transformer->expects($this->exactly(2))
  813. ->method('transform')
  814. // the method is first called with NULL, then
  815. // with $originalAuthor -> not testable by PHPUnit
  816. // ->with($this->equalTo(null))
  817. // ->with($this->equalTo($originalAuthor))
  818. ->will($this->returnValue($transformedAuthor));
  819. $form->setValueTransformer($transformer);
  820. $form->setData($originalAuthor);
  821. $field = $this->createMockField('firstName');
  822. $field->expects($this->once())
  823. ->method('writeProperty')
  824. ->with($this->equalTo($transformedAuthor));
  825. $form->add($field);
  826. $field = $this->createMockField('lastName');
  827. $field->expects($this->once())
  828. ->method('writeProperty')
  829. ->with($this->equalTo($transformedAuthor));
  830. $form->add($field);
  831. $form->submit(array()); // irrelevant
  832. }
  833. public function testGetDataReturnsObject()
  834. {
  835. $form = new Form('author');
  836. $object = new \stdClass();
  837. $form->setData($object);
  838. $this->assertEquals($object, $form->getData());
  839. }
  840. public function testGetDisplayedDataForwardsCall()
  841. {
  842. $field = $this->createValidMockField('firstName');
  843. $field->expects($this->atLeastOnce())
  844. ->method('getDisplayedData')
  845. ->will($this->returnValue('Bernhard'));
  846. $form = new Form('author');
  847. $form->add($field);
  848. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getDisplayedData());
  849. }
  850. public function testIsMultipartIfAnyFieldIsMultipart()
  851. {
  852. $form = new Form('author');
  853. $form->add($this->createMultipartMockField('firstName'));
  854. $form->add($this->createNonMultipartMockField('lastName'));
  855. $this->assertTrue($form->isMultipart());
  856. }
  857. public function testIsNotMultipartIfNoFieldIsMultipart()
  858. {
  859. $form = new Form('author');
  860. $form->add($this->createNonMultipartMockField('firstName'));
  861. $form->add($this->createNonMultipartMockField('lastName'));
  862. $this->assertFalse($form->isMultipart());
  863. }
  864. public function testSupportsClone()
  865. {
  866. $form = new Form('author');
  867. $form->add($this->createMockField('firstName'));
  868. $clone = clone $form;
  869. $this->assertNotSame($clone['firstName'], $form['firstName']);
  870. }
  871. public function testSubmitWithoutPriorSetData()
  872. {
  873. return; // TODO
  874. $field = $this->createMockField('firstName');
  875. $field->expects($this->any())
  876. ->method('getData')
  877. ->will($this->returnValue('Bernhard'));
  878. $form = new Form('author');
  879. $form->add($field);
  880. $form->submit(array('firstName' => 'Bernhard'));
  881. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getData());
  882. }
  883. public function testGetHiddenFieldsReturnsOnlyHiddenFields()
  884. {
  885. $form = $this->getGroupWithBothVisibleAndHiddenField();
  886. $hiddenFields = $form->getHiddenFields(true, false);
  887. $this->assertSame(array($form['hiddenField']), $hiddenFields);
  888. }
  889. public function testGetVisibleFieldsReturnsOnlyVisibleFields()
  890. {
  891. $form = $this->getGroupWithBothVisibleAndHiddenField();
  892. $visibleFields = $form->getVisibleFields(true, false);
  893. $this->assertSame(array($form['visibleField']), $visibleFields);
  894. }
  895. public function testValidateData()
  896. {
  897. $graphWalker = $this->createMockGraphWalker();
  898. $metadataFactory = $this->createMockMetadataFactory();
  899. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  900. $object = $this->getMock('\stdClass');
  901. $form = new Form('author', array('validation_groups' => array(
  902. 'group1',
  903. 'group2',
  904. )));
  905. $graphWalker->expects($this->exactly(2))
  906. ->method('walkReference')
  907. ->with($object,
  908. // should test for groups - PHPUnit limitation
  909. $this->anything(),
  910. 'data',
  911. true);
  912. $form->setData($object);
  913. $form->validateData($context);
  914. }
  915. public function testValidateDataAppendsPropertyPath()
  916. {
  917. $graphWalker = $this->createMockGraphWalker();
  918. $metadataFactory = $this->createMockMetadataFactory();
  919. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  920. $context->setPropertyPath('path');
  921. $object = $this->getMock('\stdClass');
  922. $form = new Form('author');
  923. $graphWalker->expects($this->once())
  924. ->method('walkReference')
  925. ->with($object,
  926. null,
  927. 'path.data',
  928. true);
  929. $form->setData($object);
  930. $form->validateData($context);
  931. }
  932. public function testValidateDataSetsCurrentPropertyToData()
  933. {
  934. $graphWalker = $this->createMockGraphWalker();
  935. $metadataFactory = $this->createMockMetadataFactory();
  936. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  937. $object = $this->getMock('\stdClass');
  938. $form = new Form('author');
  939. $test = $this;
  940. $graphWalker->expects($this->once())
  941. ->method('walkReference')
  942. ->will($this->returnCallback(function () use ($context, $test) {
  943. $test->assertEquals('data', $context->getCurrentProperty());
  944. }));
  945. $form->setData($object);
  946. $form->validateData($context);
  947. }
  948. public function testValidateDataDoesNotWalkScalars()
  949. {
  950. $graphWalker = $this->createMockGraphWalker();
  951. $metadataFactory = $this->createMockMetadataFactory();
  952. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  953. $valueTransformer = $this->createMockTransformer();
  954. $form = new Form('author', array('value_transformer' => $valueTransformer));
  955. $graphWalker->expects($this->never())
  956. ->method('walkReference');
  957. $valueTransformer->expects($this->atLeastOnce())
  958. ->method('reverseTransform')
  959. ->will($this->returnValue('foobar'));
  960. $form->submit(array('foo' => 'bar')); // reverse transformed to "foobar"
  961. $form->validateData($context);
  962. }
  963. public function testSubformDoesntCallSetters()
  964. {
  965. $author = new FormTest_AuthorWithoutRefSetter(new Author());
  966. $form = new Form('author', array('validator' => $this->createMockValidator()));
  967. $form->setData($author);
  968. $refForm = new Form('reference');
  969. $refForm->add(new TestField('firstName'));
  970. $form->add($refForm);
  971. $form->bind($this->createPostRequest(array(
  972. 'author' => array(
  973. // reference has a getter, but not setter
  974. 'reference' => array(
  975. 'firstName' => 'Foo',
  976. )
  977. )
  978. )));
  979. $this->assertEquals('Foo', $author->getReference()->firstName);
  980. }
  981. public function testSubformCallsSettersIfTheObjectChanged()
  982. {
  983. // no reference
  984. $author = new FormTest_AuthorWithoutRefSetter(null);
  985. $newReference = new Author();
  986. $form = new Form('author', array('validator' => $this->createMockValidator()));
  987. $form->setData($author);
  988. $refForm = new Form('referenceCopy');
  989. $refForm->add(new TestField('firstName'));
  990. $form->add($refForm);
  991. $refForm->setData($newReference); // new author object
  992. $form->bind($this->createPostRequest(array(
  993. 'author' => array(
  994. // referenceCopy has a getter that returns a copy
  995. 'referenceCopy' => array(
  996. 'firstName' => 'Foo',
  997. )
  998. )
  999. )));
  1000. $this->assertEquals('Foo', $author->getReferenceCopy()->firstName);
  1001. }
  1002. public function testSubformCallsSettersIfByReferenceIsFalse()
  1003. {
  1004. $author = new FormTest_AuthorWithoutRefSetter(new Author());
  1005. $form = new Form('author', array('validator' => $this->createMockValidator()));
  1006. $form->setData($author);
  1007. $refForm = new Form('referenceCopy', array('by_reference' => false));
  1008. $refForm->add(new TestField('firstName'));
  1009. $form->add($refForm);
  1010. $form->bind($this->createPostRequest(array(
  1011. 'author' => array(
  1012. // referenceCopy has a getter that returns a copy
  1013. 'referenceCopy' => array(
  1014. 'firstName' => 'Foo',
  1015. )
  1016. )
  1017. )));
  1018. // firstName can only be updated if setReferenceCopy() was called
  1019. $this->assertEquals('Foo', $author->getReferenceCopy()->firstName);
  1020. }
  1021. public function testSubformCallsSettersIfReferenceIsScalar()
  1022. {
  1023. $author = new FormTest_AuthorWithoutRefSetter('scalar');
  1024. $form = new Form('author', array('validator' => $this->createMockValidator()));
  1025. $form->setData($author);
  1026. $refForm = new FormTest_FormThatReturns('referenceCopy');
  1027. $refForm->setReturnValue('foobar');
  1028. $form->add($refForm);
  1029. $form->bind($this->createPostRequest(array(
  1030. 'author' => array(
  1031. 'referenceCopy' => array(), // doesn't matter actually
  1032. )
  1033. )));
  1034. // firstName can only be updated if setReferenceCopy() was called
  1035. $this->assertEquals('foobar', $author->getReferenceCopy());
  1036. }
  1037. public function testSubformAlwaysInsertsIntoArrays()
  1038. {
  1039. $ref1 = new Author();
  1040. $ref2 = new Author();
  1041. $author = array('referenceCopy' => $ref1);
  1042. $form = new Form('author', array('validator' => $this->createMockValidator()));
  1043. $form->setData($author);
  1044. $refForm = new FormTest_FormThatReturns('referenceCopy');
  1045. $refForm->setReturnValue($ref2);
  1046. $form->add($refForm);
  1047. $form->bind($this->createPostRequest(array(
  1048. 'author' => array(
  1049. 'referenceCopy' => array(), // doesn't matter actually
  1050. )
  1051. )));
  1052. // the new reference was inserted into the array
  1053. $author = $form->getData();
  1054. $this->assertSame($ref2, $author['referenceCopy']);
  1055. }
  1056. /**
  1057. * Create a group containing two fields, "visibleField" and "hiddenField"
  1058. *
  1059. * @return Form
  1060. */
  1061. protected function getGroupWithBothVisibleAndHiddenField()
  1062. {
  1063. $form = new Form('testGroup');
  1064. // add a visible field
  1065. $visibleField = $this->createMockField('visibleField');
  1066. $visibleField->expects($this->once())
  1067. ->method('isHidden')
  1068. ->will($this->returnValue(false));
  1069. $form->add($visibleField);
  1070. // add a hidden field
  1071. $hiddenField = $this->createMockField('hiddenField');
  1072. $hiddenField->expects($this->once())
  1073. ->method('isHidden')
  1074. ->will($this->returnValue(true));
  1075. $form->add($hiddenField);
  1076. return $form;
  1077. }
  1078. protected function createMockField($key)
  1079. {
  1080. $field = $this->getMock(
  1081. 'Symfony\Component\Form\FieldInterface',
  1082. array(),
  1083. array(),
  1084. '',
  1085. false, // don't use constructor
  1086. false // don't call parent::__clone
  1087. );
  1088. $field->expects($this->any())
  1089. ->method('getKey')
  1090. ->will($this->returnValue($key));
  1091. return $field;
  1092. }
  1093. protected function createMockForm()
  1094. {
  1095. $form = $this->getMock(
  1096. 'Symfony\Component\Form\Form',
  1097. array(),
  1098. array(),
  1099. '',
  1100. false, // don't use constructor
  1101. false // don't call parent::__clone)
  1102. );
  1103. $form->expects($this->any())
  1104. ->method('getRoot')
  1105. ->will($this->returnValue($form));
  1106. return $form;
  1107. }
  1108. protected function createInvalidMockField($key)
  1109. {
  1110. $field = $this->createMockField($key);
  1111. $field->expects($this->any())
  1112. ->method('isValid')
  1113. ->will($this->returnValue(false));
  1114. return $field;
  1115. }
  1116. protected function createValidMockField($key)
  1117. {
  1118. $field = $this->createMockField($key);
  1119. $field->expects($this->any())
  1120. ->method('isValid')
  1121. ->will($this->returnValue(true));
  1122. return $field;
  1123. }
  1124. protected function createNonMultipartMockField($key)
  1125. {
  1126. $field = $this->createMockField($key);
  1127. $field->expects($this->any())
  1128. ->method('isMultipart')
  1129. ->will($this->returnValue(false));
  1130. return $field;
  1131. }
  1132. protected function createMultipartMockField($key)
  1133. {
  1134. $field = $this->createMockField($key);
  1135. $field->expects($this->any())
  1136. ->method('isMultipart')
  1137. ->will($this->returnValue(true));
  1138. return $field;
  1139. }
  1140. protected function createMockTransformer()
  1141. {
  1142. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  1143. }
  1144. protected function createMockValidator()
  1145. {
  1146. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  1147. }
  1148. protected function createMockCsrfProvider()
  1149. {
  1150. return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
  1151. }
  1152. protected function createMockGraphWalker()
  1153. {
  1154. return $this->getMockBuilder('Symfony\Component\Validator\GraphWalker')
  1155. ->disableOriginalConstructor()
  1156. ->getMock();
  1157. }
  1158. protected function createMockMetadataFactory()
  1159. {
  1160. return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  1161. }
  1162. protected function createPostRequest(array $values = array(), array $files = array())
  1163. {
  1164. $server = array('REQUEST_METHOD' => 'POST');
  1165. return new Request(array(), $values, array(), array(), $files, $server);
  1166. }
  1167. }