WebformGeneralTestCase.test 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Test general functionality of Webform.
  4. */
  5. class WebformGeneralTestCase extends WebformTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => t('Webform'),
  12. 'description' => t('Checks global Webform settings and content types.'),
  13. 'group' => t('Webform'),
  14. );
  15. }
  16. /**
  17. * Test creating a new Webform node.
  18. */
  19. public function testWebformCreate() {
  20. $settings = array(
  21. 'title' => 'Test webform, no components',
  22. 'type' => 'webform',
  23. );
  24. $node = $this->drupalCreateNode($settings);
  25. // Because this is a "webform" type node, it should have an entry in the
  26. // database even though it's using the default settings.
  27. $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record made in the database for the new webform node.'));
  28. // Make a change to the node, ensure that the record stays intact.
  29. $node->title .= '!';
  30. node_save($node);
  31. $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record still in the database after modifying webform node.'));
  32. }
  33. /**
  34. * Test webform-enabling a different node type and testing behavior.
  35. */
  36. public function testWebformCreateNewType() {
  37. // Enable webforms on the page content type.
  38. variable_set('webform_node_webform', TRUE);
  39. variable_set('webform_node_page', TRUE);
  40. $settings = array(
  41. 'title' => 'Test webform-enabled page',
  42. 'type' => 'page',
  43. );
  44. $node = $this->drupalCreateNode($settings);
  45. // Because this is a webform-enabled type node but does not yet have any
  46. // components, it should not have an entry in the database because it is
  47. // using the default settings.
  48. $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record not in the database for the new page node.'));
  49. // Make a change to the node, ensure that the record stays empty.
  50. $node->title .= '!';
  51. node_save($node);
  52. $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record still not in the database after modifying page node.'));
  53. // Add a new component to the node and check that a record is made in the
  54. // webform table.
  55. $components = $this->webformComponents();
  56. $textarea = $components['textarea'];
  57. $textarea['type'] = 'textarea';
  58. $textarea['form_key'] = 'textarea';
  59. $textarea['cid'] = 1;
  60. $textarea['pid'] = 0;
  61. $textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
  62. $node->webform['components'][1] = $textarea;
  63. node_save($node);
  64. $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record now exists after adding a new component.'));
  65. // Remove the new component and ensure that the record is deleted.
  66. $node->webform['components'] = array();
  67. node_save($node);
  68. $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record deleted after deleting last component.'));
  69. }
  70. /**
  71. * Determine whether a Webform record exists for a given nid.
  72. *
  73. * @param int $nid
  74. * The node ID.
  75. *
  76. * @return bool
  77. * Whether or not the Webform record exists.
  78. */
  79. public function webformRecordExists($nid) {
  80. return (bool) db_query("SELECT nid FROM {webform} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
  81. }
  82. }