SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/formTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\Form;
  15.  
  16.  
  17. class formTest extends PHPUnit_Framework_TestCase
  18. {
  19.  
  20. public function testText()
  21. {
  22. Form::nextFieldDetails('Bob', true);
  23. $html = Form::text('bob');
  24.  
  25. $doc = new DOMDocument();
  26. $doc->loadHTML($html);
  27. $xml = str_replace(['<html><body>', '</body></html>'], '', $doc->saveXML());
  28.  
  29. $this->assertXmlStringEqualsXmlString(
  30. '<div class="field-element field-element--text field-element--required">
  31. <div class="field-label">
  32. <label for="field0">Bob <span class="field-label__required">required</span></label>
  33. </div>
  34. <div class="field-input">
  35. <input type="text" value="" name="bob" class="textbox" id="field0"/>
  36. </div>
  37. </div>',
  38. $xml
  39. );
  40. }
  41.  
  42. public function testWrapperClass()
  43. {
  44. Form::nextFieldDetails('Bob', true);
  45. $field = Form::text('bob', ['-wrapper-class' => 'small']);
  46. $this->assertContains('field-element--small', $field);
  47. }
  48.  
  49. public function testWrapperID()
  50. {
  51. Form::nextFieldDetails('Bob', true);
  52. $field = Form::text('bob', ['id' => 'first-name']);
  53. $this->assertContains('field-element--id-first-name', $field);
  54. }
  55.  
  56. public function testDataShallow()
  57. {
  58. Form::setData(['aaa' => '**value**']);
  59. $field = Form::text('aaa');
  60. $this->assertContains('**value**', $field);
  61. }
  62.  
  63. public function testDataShallowNameFormat()
  64. {
  65. Form::setData(['--aaa' => '**value**']);
  66. Form::setFieldNameFormat('--%s');
  67. $field = Form::text('aaa');
  68. $this->assertContains('**value**', $field);
  69. Form::setFieldNameFormat('%s');
  70. }
  71.  
  72. public function testDataDeep()
  73. {
  74. Form::setData(['aaa' => ['bbb' => '**value**']]);
  75. $field = Form::text('aaa[bbb]');
  76. $this->assertContains('**value**', $field);
  77. }
  78.  
  79. public function testDataDeepNameFormat()
  80. {
  81. Form::setData(['aaa' => ['bbb' => '**value**']]);
  82. Form::setFieldNameFormat('aaa[%s]');
  83. $field = Form::text('bbb');
  84. $this->assertContains('**value**', $field);
  85. Form::setFieldNameFormat('%s');
  86. }
  87.  
  88. public function testErrorShallow()
  89. {
  90. Form::setErrors(['aaa' => '**error**']);
  91. $field = Form::text('aaa');
  92. $this->assertContains('**error**', $field);
  93. }
  94.  
  95. public function testErrorShallowNameFormat()
  96. {
  97. Form::setErrors(['--aaa' => '**error**']);
  98. Form::setFieldNameFormat('--%s');
  99. $field = Form::text('aaa');
  100. $this->assertContains('**error**', $field);
  101. Form::setFieldNameFormat('%s');
  102. }
  103.  
  104. public function testErrorDeep1()
  105. {
  106. Form::setErrors(['aaa' => ['bbb' => '**error**']]);
  107. $field = Form::text('aaa[bbb]');
  108. $this->assertContains('**error**', $field);
  109. }
  110.  
  111. public function testErrorDeep2()
  112. {
  113. Form::setErrors(['aaa' => [5 => ['bbb' => '**error**']]]);
  114. $field = Form::text('aaa[5][bbb]');
  115. $this->assertContains('**error**', $field);
  116. }
  117.  
  118. public function testErrorDeepNameFormat()
  119. {
  120. Form::setErrors(['aaa' => ['bbb' => '**error**']]);
  121. Form::setFieldNameFormat('aaa[%s]');
  122. $field = Form::text('bbb');
  123. $this->assertContains('**error**', $field);
  124. Form::setFieldNameFormat('%s');
  125. }
  126. }
  127.