/tests/php/ORM/DBFieldTest.php

https://gitlab.com/djpmedia/silverstripe-framework · PHP · 262 lines · 206 code · 29 blank · 27 comment · 1 complexity · 609e09967549c37ce4d53af03728c881 MD5 · raw file

  1. <?php
  2. namespace SilverStripe\ORM\Tests;
  3. use SilverStripe\ORM\FieldType\DBBigInt;
  4. use SilverStripe\ORM\FieldType\DBBoolean;
  5. use SilverStripe\ORM\FieldType\DBDecimal;
  6. use SilverStripe\ORM\FieldType\DBDouble;
  7. use SilverStripe\ORM\FieldType\DBFloat;
  8. use SilverStripe\ORM\FieldType\DBHTMLText;
  9. use SilverStripe\ORM\FieldType\DBString;
  10. use SilverStripe\ORM\FieldType\DBTime;
  11. use SilverStripe\ORM\FieldType\DBVarchar;
  12. use SilverStripe\ORM\FieldType\DBText;
  13. use SilverStripe\Dev\SapphireTest;
  14. /**
  15. * Tests for DBField objects.
  16. */
  17. class DBFieldTest extends SapphireTest
  18. {
  19. /**
  20. * Test the nullValue() method on DBField.
  21. */
  22. public function testNullValue()
  23. {
  24. /* Float and Double use 0 for "null" value representation */
  25. $this->assertEquals(0, singleton('Float')->nullValue());
  26. $this->assertEquals(0, singleton('Double')->nullValue());
  27. }
  28. /**
  29. * Test the prepValueForDB() method on DBField.
  30. */
  31. public function testPrepValueForDB()
  32. {
  33. /* Float behaviour, asserting we have 0 */
  34. $float = DBFloat::create();
  35. $this->assertEquals(0, $float->prepValueForDB(0));
  36. $this->assertEquals(0, $float->prepValueForDB(null));
  37. $this->assertEquals(0, $float->prepValueForDB(false));
  38. $this->assertEquals(0, $float->prepValueForDB(''));
  39. $this->assertEquals('0', $float->prepValueForDB('0'));
  40. /* Double behaviour, asserting we have 0 */
  41. $double = DBDouble::create();
  42. $this->assertEquals(0, $double->prepValueForDB(0));
  43. $this->assertEquals(0, $double->prepValueForDB(null));
  44. $this->assertEquals(0, $double->prepValueForDB(false));
  45. $this->assertEquals(0, $double->prepValueForDB(''));
  46. $this->assertEquals('0', $double->prepValueForDB('0'));
  47. /* Integer behaviour, asserting we have 0 */
  48. $int = singleton('Int');
  49. $this->assertEquals(0, $int->prepValueForDB(0));
  50. $this->assertEquals(0, $int->prepValueForDB(null));
  51. $this->assertEquals(0, $int->prepValueForDB(false));
  52. $this->assertEquals(0, $int->prepValueForDB(''));
  53. $this->assertEquals(0, $int->prepValueForDB('0'));
  54. /* Integer behaviour, asserting we have 1 */
  55. $this->assertEquals(1, $int->prepValueForDB(true));
  56. $this->assertEquals(1, $int->prepValueForDB(1));
  57. $this->assertEquals(1, $int->prepValueForDB('1'));
  58. /* Decimal behaviour, asserting we have 0 */
  59. $decimal = DBDecimal::create();
  60. $this->assertEquals(0, $decimal->prepValueForDB(0));
  61. $this->assertEquals(0.0, $decimal->prepValueForDB(0.0));
  62. $this->assertEquals(0, $decimal->prepValueForDB(null));
  63. $this->assertEquals(0, $decimal->prepValueForDB(false));
  64. $this->assertEquals(0, $decimal->prepValueForDB(''));
  65. $this->assertEquals(0, $decimal->prepValueForDB('0'));
  66. $this->assertEquals(0.0, $decimal->prepValueForDB('0.0'));
  67. /* Decimal behaviour, asserting we have 1 */
  68. $this->assertEquals(1, $decimal->prepValueForDB(true));
  69. $this->assertEquals(1, $decimal->prepValueForDB(1));
  70. $this->assertEquals(1.1, $decimal->prepValueForDB(1.1));
  71. $this->assertEquals(1, $decimal->prepValueForDB('1'));
  72. $this->assertEquals(1.1, $decimal->prepValueForDB('1.1'));
  73. /* Boolean behaviour, asserting we have 0 */
  74. $boolean = DBBoolean::create();
  75. $this->assertEquals(false, $boolean->prepValueForDB(0));
  76. $this->assertEquals(false, $boolean->prepValueForDB(null));
  77. $this->assertEquals(false, $boolean->prepValueForDB(false));
  78. $this->assertEquals(false, $boolean->prepValueForDB('false'));
  79. $this->assertEquals(false, $boolean->prepValueForDB('f'));
  80. $this->assertEquals(false, $boolean->prepValueForDB(''));
  81. $this->assertEquals(false, $boolean->prepValueForDB('0'));
  82. /* Boolean behaviour, asserting we have 1 */
  83. $this->assertEquals(true, $boolean->prepValueForDB(true));
  84. $this->assertEquals(true, $boolean->prepValueForDB('true'));
  85. $this->assertEquals(true, $boolean->prepValueForDB('t'));
  86. $this->assertEquals(true, $boolean->prepValueForDB(1));
  87. $this->assertEquals(true, $boolean->prepValueForDB('1'));
  88. // @todo - Revisit Varchar to evaluate correct behaviour of nullifyEmpty
  89. /* Varchar behaviour: nullifyifEmpty defaults to true */
  90. $varchar = DBVarchar::create();
  91. $this->assertEquals(0, $varchar->prepValueForDB(0));
  92. $this->assertEquals(null, $varchar->prepValueForDB(null));
  93. $this->assertEquals(null, $varchar->prepValueForDB(false));
  94. $this->assertEquals(null, $varchar->prepValueForDB(''));
  95. $this->assertEquals('0', $varchar->prepValueForDB('0'));
  96. $this->assertEquals(1, $varchar->prepValueForDB(1));
  97. $this->assertEquals(true, $varchar->prepValueForDB(true));
  98. $this->assertEquals('1', $varchar->prepValueForDB('1'));
  99. $this->assertEquals('00000', $varchar->prepValueForDB('00000'));
  100. $this->assertEquals(0, $varchar->prepValueForDB(0000));
  101. $this->assertEquals('test', $varchar->prepValueForDB('test'));
  102. $this->assertEquals(123, $varchar->prepValueForDB(123));
  103. /* AllowEmpty Varchar behaviour */
  104. $varcharField = DBVarchar::create("testfield", 50, array("nullifyEmpty"=>false));
  105. $this->assertSame('0', $varcharField->prepValueForDB(0));
  106. $this->assertSame(null, $varcharField->prepValueForDB(null));
  107. $this->assertSame('', $varcharField->prepValueForDB(false));
  108. $this->assertSame('', $varcharField->prepValueForDB(''));
  109. $this->assertSame('0', $varcharField->prepValueForDB('0'));
  110. $this->assertSame('1', $varcharField->prepValueForDB(1));
  111. $this->assertSame('1', $varcharField->prepValueForDB(true));
  112. $this->assertSame('1', $varcharField->prepValueForDB('1'));
  113. $this->assertSame('00000', $varcharField->prepValueForDB('00000'));
  114. $this->assertSame('0', $varcharField->prepValueForDB(0000));
  115. $this->assertSame('test', $varcharField->prepValueForDB('test'));
  116. $this->assertSame('123', $varcharField->prepValueForDB(123));
  117. unset($varcharField);
  118. /* Text behaviour */
  119. $text = DBText::create();
  120. $this->assertEquals('0', $text->prepValueForDB(0));
  121. $this->assertEquals(null, $text->prepValueForDB(null));
  122. $this->assertEquals(null, $text->prepValueForDB(false));
  123. $this->assertEquals(null, $text->prepValueForDB(''));
  124. $this->assertEquals('0', $text->prepValueForDB('0'));
  125. $this->assertEquals('1', $text->prepValueForDB(1));
  126. $this->assertEquals('1', $text->prepValueForDB(true));
  127. $this->assertEquals('1', $text->prepValueForDB('1'));
  128. $this->assertEquals('00000', $text->prepValueForDB('00000'));
  129. $this->assertEquals('0', $text->prepValueForDB(0000));
  130. $this->assertEquals('test', $text->prepValueForDB('test'));
  131. $this->assertEquals('123', $text->prepValueForDB(123));
  132. /* AllowEmpty Text behaviour */
  133. $textField = DBText::create("testfield", array("nullifyEmpty"=>false));
  134. $this->assertSame('0', $textField->prepValueForDB(0));
  135. $this->assertSame(null, $textField->prepValueForDB(null));
  136. $this->assertSame('', $textField->prepValueForDB(false));
  137. $this->assertSame('', $textField->prepValueForDB(''));
  138. $this->assertSame('0', $textField->prepValueForDB('0'));
  139. $this->assertSame('1', $textField->prepValueForDB(1));
  140. $this->assertSame('1', $textField->prepValueForDB(true));
  141. $this->assertSame('1', $textField->prepValueForDB('1'));
  142. $this->assertSame('00000', $textField->prepValueForDB('00000'));
  143. $this->assertSame('0', $textField->prepValueForDB(0000));
  144. $this->assertSame('test', $textField->prepValueForDB('test'));
  145. $this->assertSame('123', $textField->prepValueForDB(123));
  146. unset($textField);
  147. /* Time behaviour */
  148. $time = DBTime::create();
  149. $time->setValue('12:01am');
  150. $this->assertEquals("00:01:00", $time->getValue());
  151. $time->setValue('12:59am');
  152. $this->assertEquals("00:59:00", $time->getValue());
  153. $time->setValue('11:59am');
  154. $this->assertEquals("11:59:00", $time->getValue());
  155. $time->setValue('12:00pm');
  156. $this->assertEquals("12:00:00", $time->getValue());
  157. $time->setValue('12:59am');
  158. $this->assertEquals("00:59:00", $time->getValue());
  159. $time->setValue('1:00pm');
  160. $this->assertEquals("13:00:00", $time->getValue());
  161. $time->setValue('11:59pm');
  162. $this->assertEquals("23:59:00", $time->getValue());
  163. $time->setValue('12:00am');
  164. $this->assertEquals("00:00:00", $time->getValue());
  165. $time->setValue('00:00:00');
  166. $this->assertEquals("00:00:00", $time->getValue());
  167. /* BigInt behaviour */
  168. $bigInt = DBBigInt::create();
  169. $bigInt->setValue(PHP_INT_MAX);
  170. $this->assertEquals(PHP_INT_MAX, $bigInt->getValue());
  171. }
  172. public function testExists()
  173. {
  174. $varcharField = new DBVarchar("testfield");
  175. $this->assertTrue($varcharField->getNullifyEmpty());
  176. $varcharField->setValue('abc');
  177. $this->assertTrue($varcharField->exists());
  178. $varcharField->setValue('');
  179. $this->assertFalse($varcharField->exists());
  180. $varcharField->setValue(null);
  181. $this->assertFalse($varcharField->exists());
  182. $varcharField = new DBVarchar("testfield", 50, array('nullifyEmpty'=>false));
  183. $this->assertFalse($varcharField->getNullifyEmpty());
  184. $varcharField->setValue('abc');
  185. $this->assertTrue($varcharField->exists());
  186. $varcharField->setValue('');
  187. $this->assertFalse($varcharField->exists());
  188. $varcharField->setValue(null);
  189. $this->assertFalse($varcharField->exists());
  190. $textField = new DBText("testfield");
  191. $this->assertTrue($textField->getNullifyEmpty());
  192. $textField->setValue('abc');
  193. $this->assertTrue($textField->exists());
  194. $textField->setValue('');
  195. $this->assertFalse($textField->exists());
  196. $textField->setValue(null);
  197. $this->assertFalse($textField->exists());
  198. $textField = new DBText("testfield", array('nullifyEmpty'=>false));
  199. $this->assertFalse($textField->getNullifyEmpty());
  200. $textField->setValue('abc');
  201. $this->assertTrue($textField->exists());
  202. $textField->setValue('');
  203. $this->assertFalse($textField->exists());
  204. $textField->setValue(null);
  205. $this->assertFalse($textField->exists());
  206. }
  207. public function testStringFieldsWithMultibyteData()
  208. {
  209. $plainFields = array('Varchar', 'Text');
  210. $htmlFields = array('HTMLVarchar', 'HTMLText', 'HTMLFragment');
  211. $allFields = array_merge($plainFields, $htmlFields);
  212. $value = 'üåäöÜÅÄÖ';
  213. foreach ($allFields as $stringField) {
  214. $stringField = DBString::create_field($stringField, $value);
  215. for ($i = 1; $i < mb_strlen($value); $i++) {
  216. $expected = mb_substr($value, 0, $i) . '...';
  217. $this->assertEquals($expected, $stringField->LimitCharacters($i));
  218. }
  219. }
  220. $value = '<p>üåäö&amp;ÜÅÄÖ</p>';
  221. foreach ($htmlFields as $stringField) {
  222. $stringObj = DBString::create_field($stringField, $value);
  223. // Converted to plain text
  224. $this->assertEquals('üåäö&ÜÅÄ...', $stringObj->LimitCharacters(8));
  225. // But which will be safely cast in templates
  226. $this->assertEquals('üåäö&amp;ÜÅÄ...', $stringObj->obj('LimitCharacters', [8])->forTemplate());
  227. }
  228. $this->assertEquals('ÅÄÖ', DBText::create_field('Text', 'åäö')->UpperCase());
  229. $this->assertEquals('åäö', DBText::create_field('Text', 'ÅÄÖ')->LowerCase());
  230. $this->assertEquals('<P>ÅÄÖ</P>', DBHTMLText::create_field('HTMLFragment', '<p>åäö</p>')->UpperCase());
  231. $this->assertEquals('<p>åäö</p>', DBHTMLText::create_field('HTMLFragment', '<p>ÅÄÖ</p>')->LowerCase());
  232. }
  233. }