PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/views/src/Tests/ViewTestData.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 274 lines | 245 code | 7 blank | 22 comment | 6 complexity | f871a6997edf2faf8f0a2efcc45a5d42 MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Tests;
  3. use Drupal\Core\Config\FileStorage;
  4. /**
  5. * Provides tests view data and the base test schema with sample data records.
  6. *
  7. * The methods will be used by both views test base classes.
  8. *
  9. * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase.
  10. * @see \Drupal\views\Tests\ViewTestBase.
  11. */
  12. class ViewTestData {
  13. /**
  14. * Create test views from config.
  15. *
  16. * @param string $class
  17. * The name of the test class. Installs the listed test views *in order*.
  18. * @param array $modules
  19. * The module directories to look in for test views.
  20. */
  21. public static function createTestViews($class, array $modules) {
  22. $views = [];
  23. while ($class) {
  24. if (property_exists($class, 'testViews')) {
  25. $views = array_merge($views, $class::$testViews);
  26. }
  27. $class = get_parent_class($class);
  28. }
  29. if (!empty($views)) {
  30. $storage = \Drupal::entityTypeManager()->getStorage('view');
  31. $module_handler = \Drupal::moduleHandler();
  32. foreach ($modules as $module) {
  33. $config_dir = \Drupal::service('extension.list.module')->getPath($module) . '/test_views';
  34. if (!is_dir($config_dir) || !$module_handler->moduleExists($module)) {
  35. continue;
  36. }
  37. $file_storage = new FileStorage($config_dir);
  38. $available_views = $file_storage->listAll('views.view.');
  39. foreach ($views as $id) {
  40. $config_name = 'views.view.' . $id;
  41. if (in_array($config_name, $available_views)) {
  42. $storage
  43. ->create($file_storage->read($config_name))
  44. ->save();
  45. }
  46. }
  47. }
  48. }
  49. // Rebuild the router once.
  50. \Drupal::service('router.builder')->rebuild();
  51. }
  52. /**
  53. * Returns the schema definition.
  54. *
  55. * @internal
  56. */
  57. public static function schemaDefinition() {
  58. $schema['views_test_data'] = [
  59. 'description' => 'Basic test table for Views tests.',
  60. 'fields' => [
  61. 'id' => [
  62. 'type' => 'serial',
  63. 'unsigned' => TRUE,
  64. 'not null' => TRUE,
  65. ],
  66. 'name' => [
  67. 'description' => "A person's name",
  68. 'type' => 'varchar_ascii',
  69. 'length' => 255,
  70. 'not null' => TRUE,
  71. 'default' => '',
  72. ],
  73. 'age' => [
  74. 'description' => "The person's age",
  75. 'type' => 'int',
  76. 'unsigned' => TRUE,
  77. 'not null' => TRUE,
  78. 'default' => 0,
  79. ],
  80. 'job' => [
  81. 'description' => "The person's job",
  82. 'type' => 'varchar',
  83. 'length' => 255,
  84. 'not null' => TRUE,
  85. 'default' => 'Undefined',
  86. ],
  87. 'created' => [
  88. 'description' => "The creation date of this record",
  89. 'type' => 'int',
  90. 'unsigned' => TRUE,
  91. 'not null' => TRUE,
  92. 'default' => 0,
  93. ],
  94. 'status' => [
  95. 'description' => "The status of this record",
  96. 'type' => 'int',
  97. 'unsigned' => TRUE,
  98. 'not null' => TRUE,
  99. 'default' => 0,
  100. ],
  101. ],
  102. 'primary key' => ['id'],
  103. 'unique keys' => [
  104. 'name' => ['name'],
  105. ],
  106. 'indexes' => [
  107. 'ages' => ['age'],
  108. ],
  109. ];
  110. return $schema;
  111. }
  112. /**
  113. * Returns the views data definition.
  114. */
  115. public static function viewsData() {
  116. // Declaration of the base table.
  117. $data['views_test_data']['table'] = [
  118. 'group' => 'Views test',
  119. 'base' => [
  120. 'field' => 'id',
  121. 'title' => 'Views test data',
  122. 'help' => 'Users who have created accounts on your site.',
  123. ],
  124. ];
  125. // Declaration of fields.
  126. $data['views_test_data']['id'] = [
  127. 'title' => 'ID',
  128. 'help' => 'The test data ID',
  129. 'field' => [
  130. 'id' => 'numeric',
  131. ],
  132. 'argument' => [
  133. 'id' => 'numeric',
  134. ],
  135. 'filter' => [
  136. 'id' => 'numeric',
  137. ],
  138. 'sort' => [
  139. 'id' => 'standard',
  140. ],
  141. ];
  142. $data['views_test_data']['name'] = [
  143. 'title' => 'Name',
  144. 'help' => 'The name of the person',
  145. 'field' => [
  146. 'id' => 'standard',
  147. ],
  148. 'argument' => [
  149. 'id' => 'string',
  150. ],
  151. 'filter' => [
  152. 'id' => 'string',
  153. ],
  154. 'sort' => [
  155. 'id' => 'standard',
  156. ],
  157. ];
  158. $data['views_test_data']['age'] = [
  159. 'title' => 'Age',
  160. 'help' => 'The age of the person',
  161. 'field' => [
  162. 'id' => 'numeric',
  163. ],
  164. 'argument' => [
  165. 'id' => 'numeric',
  166. ],
  167. 'filter' => [
  168. 'id' => 'numeric',
  169. ],
  170. 'sort' => [
  171. 'id' => 'standard',
  172. ],
  173. ];
  174. $data['views_test_data']['job'] = [
  175. 'title' => 'Job',
  176. 'help' => 'The job of the person',
  177. 'field' => [
  178. 'id' => 'standard',
  179. ],
  180. 'argument' => [
  181. 'id' => 'string',
  182. ],
  183. 'filter' => [
  184. 'id' => 'string',
  185. ],
  186. 'sort' => [
  187. 'id' => 'standard',
  188. ],
  189. ];
  190. $data['views_test_data']['created'] = [
  191. 'title' => 'Created',
  192. 'help' => 'The creation date of this record',
  193. 'field' => [
  194. 'id' => 'date',
  195. ],
  196. 'argument' => [
  197. 'id' => 'date',
  198. ],
  199. 'filter' => [
  200. 'id' => 'date',
  201. ],
  202. 'sort' => [
  203. 'id' => 'date',
  204. ],
  205. ];
  206. $data['views_test_data']['status'] = [
  207. 'title' => 'Status',
  208. 'help' => 'The status of this record',
  209. 'field' => [
  210. 'id' => 'boolean',
  211. ],
  212. 'filter' => [
  213. 'id' => 'boolean',
  214. ],
  215. 'sort' => [
  216. 'id' => 'standard',
  217. ],
  218. ];
  219. return $data;
  220. }
  221. /**
  222. * Returns a very simple test dataset.
  223. */
  224. public static function dataSet() {
  225. return [
  226. [
  227. 'name' => 'John',
  228. 'age' => 25,
  229. 'job' => 'Singer',
  230. 'created' => gmmktime(0, 0, 0, 1, 1, 2000),
  231. 'status' => 1,
  232. ],
  233. [
  234. 'name' => 'George',
  235. 'age' => 27,
  236. 'job' => 'Singer',
  237. 'created' => gmmktime(0, 0, 0, 1, 2, 2000),
  238. 'status' => 0,
  239. ],
  240. [
  241. 'name' => 'Ringo',
  242. 'age' => 28,
  243. 'job' => 'Drummer',
  244. 'created' => gmmktime(6, 30, 30, 1, 1, 2000),
  245. 'status' => 1,
  246. ],
  247. [
  248. 'name' => 'Paul',
  249. 'age' => 26,
  250. 'job' => 'Songwriter',
  251. 'created' => gmmktime(6, 0, 0, 1, 1, 2000),
  252. 'status' => 0,
  253. ],
  254. [
  255. 'name' => 'Meredith',
  256. 'age' => 30,
  257. 'job' => 'Speaker',
  258. 'created' => gmmktime(6, 30, 10, 1, 1, 2000),
  259. 'status' => 1,
  260. ],
  261. ];
  262. }
  263. }