serial.test 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * Test Serial functionality.
  5. */
  6. /**
  7. * Class SerialTestCase.
  8. */
  9. class SerialTestCase extends DrupalWebTestCase {
  10. const CONTENT_TYPE = 'serial_test_content_type';
  11. /**
  12. * @var \SerialFields
  13. */
  14. private $fields;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static function getInfo() {
  19. return array(
  20. 'name' => 'Serial Field',
  21. 'group' => 'Field',
  22. 'description' => 'Testing serial field functionality.',
  23. );
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function setUp() {
  29. parent::setUp('serial', 'comment');
  30. $this->drupalCreateContentType(array('type' => static::CONTENT_TYPE));
  31. $this->fields = new SerialFields(array(
  32. 'serial' => array(
  33. 'type' => SERIAL_FIELD_TYPE,
  34. 'label' => 'Serial',
  35. 'settings' => array(),
  36. ),
  37. ));
  38. $this->fields
  39. ->create()
  40. // Attach serial field to content type and comments form.
  41. ->attach('node', static::CONTENT_TYPE)
  42. ->attach('comment', 'comment_node_' . static::CONTENT_TYPE);
  43. // Grant all known permissions for user.
  44. $this->drupalLogin($this->drupalCreateUser(array_keys(module_invoke_all('permission'))));
  45. }
  46. /**
  47. * Create N nodes and attach N comments for the last.
  48. *
  49. * @param int $nodes
  50. * Number of nodes for creation.
  51. * @param int $comments
  52. * Number of comments for creation.
  53. */
  54. public function testSerial($nodes = 15, $comments = 6) {
  55. for ($i = 0; $i < $nodes; $i++) {
  56. // Open form for add new node.
  57. $this->visit('node/add/' . str_replace('_', '-', static::CONTENT_TYPE));
  58. // Submit new node with filled title.
  59. $this->drupalPost(NULL, array('title' => "Node $i"), t('Save'));
  60. }
  61. // Go to editing of the last created node.
  62. $this->visit("node/$nodes/edit");
  63. // Check that last created node number equal to serial ID.
  64. $this->assertSerialField($nodes);
  65. // Go to viewing of the last created node.
  66. $this->visit("node/$nodes");
  67. // Post comments for last created node.
  68. for ($i = 0; $i < $comments; $i++) {
  69. $this->drupalPost(NULL, array(self::fieldName('comment_body') => "Comment $i"), t('Save'));
  70. }
  71. // Go to editing of the last created comment.
  72. $this->visit("comment/$comments/edit");
  73. // Ensure the last-posted comment number equal to serial ID.
  74. $this->assertSerialField($comments);
  75. }
  76. /**
  77. * Assert number with value of the serial field on the page.
  78. *
  79. * @param int $number
  80. * The number for verification.
  81. */
  82. private function assertSerialField($number) {
  83. $this->assertFieldByXPath($this->constructFieldXpath('name', self::fieldName('serial')), $number);
  84. }
  85. /**
  86. * Visit path and assert response code.
  87. *
  88. * @param string $path
  89. * Path to visit.
  90. * @param int $code
  91. * Expected response code.
  92. */
  93. private function visit($path, $code = 200) {
  94. $this->drupalGet($path);
  95. $this->assertResponse($code);
  96. }
  97. /**
  98. * Convert Drupal field name into HTML.
  99. *
  100. * @param string $name
  101. * Drupal field name.
  102. * @param string $column
  103. * Field column.
  104. *
  105. * @return string
  106. * HTML input name.
  107. */
  108. private static function fieldName($name, $column = 'value') {
  109. return $name . '[' . LANGUAGE_NONE . '][0][' . $column . ']';
  110. }
  111. }