paragraphs.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Holds test for paragraphs.
  5. */
  6. /**
  7. * Defines tests for paragraphs.
  8. */
  9. class ParagraphsWebTestCase extends DrupalWebTestCase {
  10. protected $privilegedUser;
  11. /**
  12. * Give display information to the SimpleTest system.
  13. *
  14. * getInfo() returns a keyed array of information for SimpleTest to show.
  15. *
  16. * It's a good idea to organize your tests consistently using the 'group'
  17. * key.
  18. */
  19. public static function getInfo() {
  20. return array(
  21. 'name' => 'Paragraphs test',
  22. 'description' => 'Ensure that the simpletest_example content type provided functions properly.',
  23. 'group' => 'Paragraphs',
  24. );
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function setUp() {
  30. $modules = array(
  31. 'paragraphs_test',
  32. );
  33. parent::setUp($modules);
  34. // Make sure the base configuration is set up.
  35. features_revert_module('paragraphs_test');
  36. // Create with a user rolegiven by the feature.
  37. $this->privilegedUser = $this->drupalCreateUserWithRole('ptest creator');
  38. }
  39. /**
  40. * Tests creating and updating a node with panelizer enabled paragraphs.
  41. */
  42. public function testPanelizer() {
  43. $this->drupalLogin($this->privilegedUser);
  44. $this->drupalGet('node/add/paragraph-test');
  45. // Add a new paragraph before saving node.
  46. $this->drupalPost(NULL, array(), t('Add new Paragraph'));
  47. $title = $this->randomString(20);
  48. $value1 = $this->randomString(20);
  49. $create_edit = array(
  50. 'title' => $title,
  51. 'field_paragraphs[und][0][field_ptest_text][und][0][value]' => $value1,
  52. );
  53. $this->drupalPost(NULL, $create_edit, t('Save'));
  54. $this->assertRaw(t('!post %title has been created.', array('!post' => 'Paragraph Test', '%title' => $title)), 'Paragraph test node created.');
  55. $this->assertText(check_plain($value1), 'First value of paragraph was rendered.');
  56. // Update the created node.
  57. $node_url = $this->getUrl();
  58. $this->drupalGet($node_url . '/edit');
  59. $this->drupalPost(NULL, array(), t('Add another Paragraph'));
  60. $value2 = $this->randomString(20);
  61. $update_edit = array(
  62. 'field_paragraphs[und][1][field_ptest_text][und][0][value]' => $value2,
  63. );
  64. $this->drupalPost(NULL, $update_edit, t('Save'));
  65. $this->assertRaw(t('!post %title has been updated.', array('!post' => 'Paragraph Test', '%title' => $title)), 'Paragraph test node updated.');
  66. $this->assertText(check_plain($value1), 'First value of paragraph was rendered.');
  67. $this->assertText(check_plain($value2), 'Second value of paragraph was rendered.');
  68. }
  69. /**
  70. * Helper to create a user with a given role.
  71. *
  72. * @param $role_name
  73. * @return bool|\stdClass
  74. * @throws \Exception
  75. *
  76. * @see DrupalWebTestCase::drupalCreateUser()
  77. */
  78. protected function drupalCreateUserWithRole($role_name) {
  79. $role = user_role_load_by_name($role_name);
  80. if (!$role) {
  81. return FALSE;
  82. }
  83. // Create a user assigned to that role.
  84. $edit = array();
  85. $edit['name'] = $this->randomName();
  86. $edit['mail'] = $edit['name'] . '@example.com';
  87. $edit['pass'] = user_password();
  88. $edit['status'] = 1;
  89. $edit['roles'] = array($role->rid => $role->rid);
  90. $account = user_save(drupal_anonymous_user(), $edit);
  91. $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
  92. if (empty($account->uid)) {
  93. return FALSE;
  94. }
  95. // Add the raw password so that we can log in as this user.
  96. $account->pass_raw = $edit['pass'];
  97. return $account;
  98. }
  99. }