PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 311 lines | 138 code | 29 blank | 144 comment | 6 complexity | ad965e24ac9131326cf648054dba9833 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\text\Tests\Formatter\TextPlainUnitTest.
  5. */
  6. namespace Drupal\text\Tests\Formatter;
  7. use Drupal\Core\Entity\ContentEntityInterface;
  8. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  9. use Drupal\Core\Language\Language;
  10. use Drupal\simpletest\DrupalUnitTestBase;
  11. /**
  12. * Tests the text_plain field formatter.
  13. *
  14. * @todo Move assertion helper methods into DrupalUnitTestBase.
  15. * @todo Move field helper methods, $modules, and setUp() into a new
  16. * FieldPluginUnitTestBase.
  17. */
  18. class TextPlainUnitTest extends DrupalUnitTestBase {
  19. /**
  20. * Modules to enable.
  21. *
  22. * @var array
  23. */
  24. public static $modules = array('entity', 'field', 'text', 'entity_test', 'system', 'filter', 'user');
  25. /**
  26. * Contains rendered content.
  27. *
  28. * @var string
  29. */
  30. protected $content;
  31. public static function getInfo() {
  32. return array(
  33. 'name' => 'Text field text_plain formatter',
  34. 'description' => "Test the creation of text fields.",
  35. 'group' => 'Field types',
  36. );
  37. }
  38. function setUp() {
  39. parent::setUp();
  40. // Configure the theme system.
  41. $this->installConfig(array('system', 'field'));
  42. $this->installSchema('entity_test', 'entity_test');
  43. // @todo Add helper methods for all of the following.
  44. $this->entity_type = 'entity_test';
  45. if (!isset($this->bundle)) {
  46. $this->bundle = $this->entity_type;
  47. }
  48. $this->field_name = drupal_strtolower($this->randomName());
  49. $this->field_type = 'text_long';
  50. $this->field_settings = array();
  51. $this->instance_settings = array(
  52. 'text_processing' => FALSE,
  53. );
  54. $this->formatter_type = 'text_plain';
  55. $this->formatter_settings = array();
  56. $this->field = entity_create('field_entity', array(
  57. 'name' => $this->field_name,
  58. 'entity_type' => $this->entity_type,
  59. 'type' => $this->field_type,
  60. 'settings' => $this->field_settings,
  61. ));
  62. $this->field->save();
  63. $this->instance = entity_create('field_instance', array(
  64. 'entity_type' => $this->entity_type,
  65. 'bundle' => $this->bundle,
  66. 'field_name' => $this->field_name,
  67. 'label' => $this->randomName(),
  68. 'settings' => $this->instance_settings,
  69. ));
  70. $this->instance->save();
  71. $this->view_mode = 'default';
  72. $this->display = entity_get_display($this->entity_type, $this->bundle, $this->view_mode)
  73. ->setComponent($this->field_name, array(
  74. 'type' => $this->formatter_type,
  75. 'settings' => $this->formatter_settings,
  76. ));
  77. $this->display->save();
  78. $this->langcode = Language::LANGCODE_NOT_SPECIFIED;
  79. }
  80. /**
  81. * Creates an entity of type $this->entity_type and bundle $this->bundle.
  82. *
  83. * @param array $values
  84. * (optional) Additional values to pass to entity_create().
  85. *
  86. * @return \Drupal\Core\Entity\EntityInterface
  87. * The created entity object.
  88. */
  89. protected function createEntity($values = array()) {
  90. $entity_type = \Drupal::entityManager()->getDefinition($this->entity_type);
  91. $bundle_key = $entity_type->getKey('bundle');
  92. $entity = entity_create($this->entity_type, $values + array(
  93. $bundle_key => $this->bundle,
  94. ));
  95. return $entity;
  96. }
  97. /**
  98. * Renders fields of a given entity with a given display.
  99. *
  100. * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  101. * The entity object with attached fields to render.
  102. * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
  103. * The display to render the fields in.
  104. */
  105. protected function renderEntityFields(ContentEntityInterface $entity, EntityViewDisplayInterface $display) {
  106. $content = $display->build($entity);
  107. $this->content = drupal_render($content);
  108. return $this->content;
  109. }
  110. /**
  111. * Formats an assertion message string.
  112. *
  113. * Unlike format_string(),
  114. * - all replacement tokens are exported via var_export() and sanitized for
  115. * output, regardless of token type used (i.e., '@', '!', and '%' do not
  116. * have any special meaning).
  117. * - Replacement token values containing newlines are automatically wrapped
  118. * into a PRE element to aid in debugging test failures.
  119. *
  120. * @param string $message
  121. * The assertion message string containing placeholders.
  122. * @param array $args
  123. * An array of replacement token values to inject into $message.
  124. *
  125. * @return string
  126. * The $message with exported replacement tokens, sanitized for HTML output.
  127. *
  128. * @see check_plain()
  129. * @see format_string()
  130. */
  131. protected function formatString($message, array $args) {
  132. array_walk($args, function (&$value) {
  133. // Export/dump the raw replacement token value.
  134. $value = var_export($value, TRUE);
  135. // Sanitize the value for output.
  136. $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  137. // Wrap the value in a PRE element if it contains newlines.
  138. if (strpos($value, "\n")) {
  139. $value = '<pre style="white-space: pre-wrap;">' . $value . '</pre>';
  140. }
  141. });
  142. return strtr($message, $args);
  143. }
  144. /**
  145. * Gets the plain-text version of $this->content.
  146. *
  147. * @param string $content
  148. * A (HTML) string.
  149. *
  150. * @return string
  151. * The $content with all HTML tags stripped and all HTML entities replaced
  152. * with their actual characters.
  153. */
  154. protected function getPlainTextContent($content) {
  155. // Strip all HTML tags.
  156. $content = strip_tags($content);
  157. // Decode all HTML entities (e.g., '&nbsp;') into characters.
  158. $content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
  159. return $content;
  160. }
  161. /**
  162. * Asserts that a raw string appears in $this->content.
  163. *
  164. * @param string $value
  165. * The raw string to look for.
  166. * @param string $message
  167. * (optional) An assertion message. If omitted, a default message is used.
  168. * Available placeholders:
  169. * - @value: The $value.
  170. * - @content: The current value of $this->content.
  171. * @param array $args
  172. * (optional) Additional replacement token map to pass to formatString().
  173. *
  174. * @return bool
  175. * TRUE on pass, FALSE on fail.
  176. */
  177. protected function assertRaw($value, $message = NULL, $args = array()) {
  178. if (!isset($message)) {
  179. $message = 'Raw string @value found in @content';
  180. }
  181. $args += array(
  182. '@value' => $value,
  183. '@content' => $this->content,
  184. );
  185. return $this->assert(strpos($this->content, $value) !== FALSE, $this->formatString($message, $args));
  186. }
  187. /**
  188. * Asserts that a raw string does not appear in $this->content.
  189. *
  190. * @param string $value
  191. * The raw string to look for.
  192. * @param string $message
  193. * (optional) An assertion message. If omitted, a default message is used.
  194. * Available placeholders:
  195. * - @value: The $value.
  196. * - @content: The current value of $this->content.
  197. * @param array $args
  198. * (optional) Additional replacement token map to pass to formatString().
  199. *
  200. * @return bool
  201. * TRUE on pass, FALSE on fail.
  202. */
  203. protected function assertNoRaw($value, $message = NULL, $args = array()) {
  204. if (!isset($message)) {
  205. $message = 'Raw string @value not found in @content';
  206. }
  207. $args += array(
  208. '@value' => $value,
  209. '@content' => $this->content,
  210. );
  211. return $this->assert(strpos($this->content, $value) === FALSE, $this->formatString($message, $args));
  212. }
  213. /**
  214. * Asserts that a text string appears in the text-only version of $this->content.
  215. *
  216. * @param string $value
  217. * The text string to look for.
  218. * @param string $message
  219. * (optional) An assertion message. If omitted, a default message is used.
  220. * Available placeholders:
  221. * - @value: The $value.
  222. * - @content: The current value of $this->content.
  223. * @param array $args
  224. * (optional) Additional replacement token map to pass to formatString().
  225. *
  226. * @return bool
  227. * TRUE on pass, FALSE on fail.
  228. */
  229. protected function assertText($value, $message = NULL, $args = array()) {
  230. if (!isset($message)) {
  231. $message = 'Text string @value found in @content';
  232. }
  233. $content = $this->getPlainTextContent($this->content);
  234. $args += array(
  235. '@value' => $value,
  236. '@content' => $content,
  237. );
  238. return $this->assert(strpos($content, $value) !== FALSE, $this->formatString($message, $args));
  239. }
  240. /**
  241. * Asserts that a text string does not appear in the text-only version of $this->content.
  242. *
  243. * @param string $value
  244. * The text string to look for.
  245. * @param string $message
  246. * (optional) An assertion message. If omitted, a default message is used.
  247. * Available placeholders:
  248. * - @value: The $value.
  249. * - @content: The current value of $this->content.
  250. * @param array $args
  251. * (optional) Additional replacement token map to pass to formatString().
  252. *
  253. * @return bool
  254. * TRUE on pass, FALSE on fail.
  255. */
  256. protected function assertNoText($value, $message = NULL, $args = array()) {
  257. if (!isset($message)) {
  258. $message = 'Text string @value not found in @content';
  259. }
  260. $content = $this->getPlainTextContent($this->content);
  261. $args += array(
  262. '@value' => $value,
  263. '@content' => $content,
  264. );
  265. return $this->assert(strpos($content, $value) === FALSE, $this->formatString($message, $args));
  266. }
  267. /**
  268. * Tests text_plain formatter output.
  269. */
  270. function testPlainText() {
  271. $value = $this->randomString();
  272. $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
  273. $value .= "\n\n" . $this->randomString();
  274. $entity = $this->createEntity(array());
  275. $entity->{$this->field_name}->value = $value;
  276. // Verify that all HTML is escaped and newlines are retained.
  277. $this->renderEntityFields($entity, $this->display);
  278. $this->assertText($value);
  279. $this->assertNoRaw($value);
  280. $this->assertRaw(nl2br(check_plain($value)));
  281. }
  282. }