publishcontent.test 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for Publish Content module.
  5. */
  6. /**
  7. * We test to ensure we are not messing up with the default Drupal
  8. * access for view node i.e. a owner of a node can view it even if unpublished.
  9. */
  10. abstract class PublishContentWebTestBase extends DrupalWebTestCase {
  11. /**
  12. * Perform a GET operation on a node.
  13. *
  14. * This will check the response to access some operation via
  15. * the URL of a node. In the case of 'publish' or 'unpublish'
  16. * it will first visit the view of a node so that the relevant
  17. * tabs can be generated.
  18. *
  19. * @param int $nid
  20. * The node nid
  21. * @param string $op
  22. * An operation such as 'view', 'edit', 'publish', 'unpublish'
  23. * @param int $expected_response
  24. * The expexted response code. If the user should not be able to
  25. * see the 'publish' or 'unpublish' tabs, set this to 403, otherwise
  26. * 200.
  27. * @param string $msg
  28. * (optional) An assertion log message.
  29. */
  30. public abstract function assertNodeOperationAccess($nid, $op, $expected_response, $msg = '');
  31. /**
  32. * Allow content type to be used with publish content.
  33. */
  34. public function enablePublishContentForContentType($types) {
  35. if (!is_array($types)) {
  36. $types = array($types);
  37. }
  38. foreach ($types as $type) {
  39. variable_set('publishcontent_' . $type, TRUE);
  40. }
  41. // Rebuild SimpleTests permissions cache.
  42. $this->checkPermissions(array(), TRUE);
  43. }
  44. /**
  45. * Assert the status of a given node.
  46. *
  47. * @param int $nid
  48. * The node nid to check
  49. * @param int $status
  50. * Either 1 for published or 0 for unpublished
  51. */
  52. public function assertNodeStatus($nid, $status, $msg = '') {
  53. $node = node_load($nid, NULL, TRUE);
  54. $msg = !empty($msg) ? $msg : t('Node status is @actual, expecting @expected', array('@actual' => $node->status, '@expected' => $status));
  55. $this->assertEqual($node->status, $status, $msg);
  56. }
  57. /**
  58. * Set the status of a node.
  59. *
  60. * @param node|int $node
  61. * A loaded node object or node nid
  62. * @param int $status
  63. * The status to set, 1 for published, 0 for unpublished.
  64. */
  65. public function setNodeStatus($node, $status, $msg = '') {
  66. $node = is_object($node) ? $node : node_load($node);
  67. if ($node->status != $status) {
  68. $node->status = $status;
  69. node_save($node);
  70. }
  71. }
  72. /**
  73. * Check the current user session forbids publish of a given node.
  74. *
  75. * @param node $node
  76. * The node object to test against.
  77. */
  78. public function assertCurrentUserCannotPublish($node) {
  79. $status = $node->status;
  80. $username = $this->loggedInUser->name;
  81. $this->setNodeStatus($node, 1, 'Start test with a published node');
  82. $this->assertNodeOperationAccess($node->nid, 'publish', 403, $username . ' cannot publish the published node');
  83. $this->assertNodeStatus($node->nid, 1, 'node should be still published');
  84. $this->assertNodeOperationAccess($node->nid, 'view', 200, $username . ' can view the published node');
  85. $this->setNodeStatus($node, 0);
  86. $this->assertNodeOperationAccess($node->nid, 'publish', 403, $username . ' cannot publish the unpublished node');
  87. $this->assertNodeStatus($node->nid, 0, 'node should be still unpublished');
  88. $this->setNodeStatus($node, $status, 'Reset the nodes status');
  89. }
  90. /**
  91. * Check the current user session cannot unpublish a given node.
  92. *
  93. * @param node $node
  94. * The node object to test against.
  95. */
  96. public function assertCurrentUserCannotUnpublish($node) {
  97. $status = $node->status;
  98. $username = $this->loggedInUser->name;
  99. $this->setNodeStatus($node, 1, 'Start test with a published node');
  100. $this->assertNodeOperationAccess($node->nid, 'unpublish', 403, $username . ' cannot unpublish the published node');
  101. $this->assertNodeStatus($node->nid, 1, 'node should be still published');
  102. $this->assertNodeOperationAccess($node->nid, 'view', 200, $username . ' can view the published node');
  103. $this->setNodeStatus($node, 0);
  104. $this->assertNodeOperationAccess($node->nid, 'unpublish', 403, $username . ' cannot unpublish an unpublished node');
  105. $this->assertNodeStatus($node->nid, 0, 'node should be still unpublished');
  106. $this->setNodeStatus($node, $status, 'Reset the nodes status');
  107. }
  108. /**
  109. * Check the current user session can publish a given node.
  110. *
  111. * @param node $node
  112. * The node object to test against.
  113. */
  114. public function assertCurrentUserCanPublish($node) {
  115. $status = $node->status;
  116. $username = $this->loggedInUser->name;
  117. $this->setNodeStatus($node, 1, 'Start test with a published node');
  118. $this->assertNodeOperationAccess($node->nid, 'publish', 403, $username . ' cannot access the publish callback for a node which is already published.');
  119. $this->assertNodeOperationAccess($node->nid, 'view', 200, $username . ' can view the published node');
  120. $this->setNodeStatus($node, 0);
  121. $this->assertNodeOperationAccess($node->nid, 'view', 200, $username . ' can view unpublished node');
  122. $this->assertNodeOperationAccess($node->nid, 'publish', 200, $username . ' can access publish callback on an unpublished node');
  123. $this->assertNodeStatus($node->nid, 1, 'node should now be published');
  124. $this->setNodeStatus($node, $status, 'Reset the nodes status');
  125. }
  126. /**
  127. * Check the current user session can unpublish a node.
  128. *
  129. * @param node $node
  130. * The node to test against.
  131. */
  132. public function assertCurrentUserCanUnpublish($node) {
  133. $status = $node->status;
  134. $username = $this->loggedInUser->name;
  135. $this->setNodeStatus($node, 1, 'Start test with a published node');
  136. $this->assertNodeOperationAccess($node->nid, 'unpublish', 200, $username . ' can access unpublish callback on a published node');
  137. $this->assertNodeStatus($node->nid, 0, 'Node should now be unpublished');
  138. $this->assertNodeOperationAccess($node->nid, 'view', 200, $username . ' can view the unpublished node.');
  139. $this->assertNodeOperationAccess($node->nid, 'unpublish', 403, $username . ' cannot access the unpublish callback of an unpublished node');
  140. $this->assertNodeStatus($node->nid, 0, 'Node should still be unpublished');
  141. $this->setNodeStatus($node, $status, 'Reset the nodes status');
  142. }
  143. /**
  144. * Assert the current user can publish a node from the listing test page.
  145. */
  146. public function assertCanPublishFromLinksPage($node) {
  147. $status = $node->status;
  148. $this->setNodeStatus($node, 0, 'Start test with an unpublished node');
  149. $this->drupalGet('publishcontent-links');
  150. $this->assertResponse(200);
  151. $this->assertLink('publish-' . $node->nid);
  152. $this->assertNoLink('unpublish-' . $node->nid);
  153. $this->clickLink('publish-' . $node->nid);
  154. $this->assertResponse(200);
  155. $this->assertNodeStatus($node->nid, 1);
  156. $this->setNodeStatus($node, $status, 'Reset status');
  157. }
  158. /**
  159. * Assert the current user cannot publish a node from the listing test page.
  160. */
  161. public function assertCannotPublishFromLinksPage($node) {
  162. $status = $node->status;
  163. $this->setNodeStatus($node, 0, 'Start test with an unpublished node');
  164. $this->drupalGet('publishcontent-links');
  165. $this->assertResponse(200);
  166. $this->assertNoLink('publish-' . $node->nid);
  167. $this->assertNoLink('unpublish-' . $node->nid);
  168. $this->setNodeStatus($node, $status, 'Reset status');
  169. }
  170. /**
  171. * Assert the current user can unpublish a node from the listing test page.
  172. */
  173. public function assertCanUnpublishFromLinksPage($node) {
  174. $status = $node->status;
  175. $this->setNodeStatus($node, 1, 'Start test with a published node');
  176. $this->drupalGet('publishcontent-links');
  177. $this->assertResponse(200);
  178. $this->assertLink('unpublish-' . $node->nid);
  179. $this->assertNoLink('publish-' . $node->nid);
  180. $this->clickLink('unpublish-' . $node->nid);
  181. $this->assertResponse(200);
  182. $this->assertNodeStatus($node->nid, 0);
  183. $this->setNodeStatus($node, $status, 'Reset status');
  184. }
  185. /**
  186. * Assert the current user cannot unpublish a node from the listing test page.
  187. */
  188. public function assertCannotUnpublishFromLinksPage($node) {
  189. $status = $node->status;
  190. $this->setNodeStatus($node, 1, 'Start test with a published node');
  191. $this->drupalGet('publishcontent-links');
  192. $this->assertResponse(200);
  193. $this->assertNoLink('publish-' . $node->nid);
  194. $this->assertNoLink('unpublish-' . $node->nid);
  195. $this->setNodeStatus($node, $status, 'Reset status');
  196. }
  197. /**
  198. * Check no publish permission by node owner.
  199. */
  200. public function testNoPublishPermissionByOwner() {
  201. $web_user = $this->drupalCreateUser(array('access content'));
  202. $this->drupalLogin($web_user);
  203. $node = $this->drupalCreateNode(
  204. array(
  205. 'type' => 'page',
  206. 'uid' => $web_user->uid,
  207. 'status' => 1,
  208. )
  209. );
  210. $this->assertCurrentUserCannotPublish($node);
  211. $this->assertCurrentUserCannotUnpublish($node);
  212. $this->setNodeStatus($node, 0);
  213. $this->assertNodeOperationAccess($node->nid, 'view', 403, 'Node is not accessible by its owner when unpublished.');
  214. }
  215. /**
  216. * Check publishcontent module does not interfere with the normal Drupal.
  217. */
  218. public function testNoPermissionAndNotOwner() {
  219. $node = $this->drupalCreateNode(
  220. array(
  221. 'type' => 'page',
  222. 'uid' => 0,
  223. 'status' => 1,
  224. )
  225. );
  226. $this->drupalLogin($this->drupalCreateUser(array('access content')));
  227. $this->assertCurrentUserCannotPublish($node);
  228. $this->assertCurrentUserCannotUnpublish($node);
  229. $this->setNodeStatus($node, 0);
  230. $this->assertNodeOperationAccess($node->nid, 'view', 403, 'Node is not viewable by non owner when unpublished by a user without publish or unpublish permissions');
  231. }
  232. /**
  233. * Test the combination of publish but not unpublish permissions.
  234. */
  235. public function testPublishNotUnpublish() {
  236. $type = 'page';
  237. $this->enablePublishContentForContentType($type);
  238. $web_user = $this->drupalCreateUser(array(
  239. 'access content',
  240. 'publish editable ' . $type . ' content',
  241. 'view own unpublished content',
  242. 'edit own ' . $type . ' content',
  243. ));
  244. $this->drupalLogin($web_user);
  245. $node = $this->drupalCreateNode(
  246. array(
  247. 'type' => $type,
  248. 'uid' => $web_user->uid,
  249. 'status' => 0,
  250. )
  251. );
  252. $this->drupalGet('node/' . $node->nid . '/edit');
  253. $this->assertResponse(200);
  254. $this->assertCurrentUserCanPublish($node);
  255. $this->assertCurrentUserCannotUnpublish($node);
  256. }
  257. /**
  258. * Test the combination of unpublish but not publish.
  259. */
  260. public function testNotPublishUnpublish() {
  261. $type = 'page';
  262. $this->enablePublishContentForContentType($type);
  263. $web_user = $this->drupalCreateUser(array(
  264. 'access content',
  265. 'unpublish any ' . $type . ' content',
  266. 'view own unpublished content',
  267. 'edit own ' . $type . ' content',
  268. ));
  269. $this->drupalLogin($web_user);
  270. $node = $this->drupalCreateNode(
  271. array(
  272. 'type' => $type,
  273. 'uid' => $web_user->uid,
  274. 'status' => 1,
  275. )
  276. );
  277. $this->assertCurrentUserCannotPublish($node);
  278. $this->assertCurrentUserCanUnpublish($node);
  279. }
  280. /**
  281. * Test the combination of both publish and unpublish.
  282. */
  283. public function testPublishUnpublish() {
  284. $type = 'page';
  285. $this->enablePublishContentForContentType($type);
  286. $web_user_1 = $this->drupalCreateUser(array(
  287. 'access content',
  288. 'view own unpublished content',
  289. 'edit any ' . $type . ' content',
  290. 'publish any content',
  291. ));
  292. $node1 = $this->drupalCreateNode(
  293. array(
  294. 'type' => $type,
  295. 'uid' => $web_user_1->uid,
  296. 'status' => 1,
  297. )
  298. );
  299. $this->drupalLogin($web_user_1);
  300. $this->assertCurrentUserCanPublish($node1);
  301. $this->assertCurrentUserCannotUnpublish($node1);
  302. $web_user_2 = $this->drupalCreateUser(array(
  303. 'access content',
  304. 'view own unpublished content',
  305. 'edit any ' . $type . ' content',
  306. 'unpublish any content',
  307. ));
  308. $node2 = $this->drupalCreateNode(
  309. array(
  310. 'type' => $type,
  311. 'uid' => $web_user_2->uid,
  312. 'status' => 1,
  313. )
  314. );
  315. $this->drupalLogin($web_user_2);
  316. $this->assertCurrentUserCannotPublish($node2);
  317. $this->assertCurrentUserCanUnpublish($node2);
  318. $web_user_3 = $this->drupalCreateUser(array(
  319. 'access content',
  320. 'view own unpublished content',
  321. 'edit any ' . $type . ' content',
  322. 'publish any content',
  323. 'unpublish any content',
  324. ));
  325. $node3 = $this->drupalCreateNode(
  326. array(
  327. 'type' => $type,
  328. 'uid' => $web_user_3->uid,
  329. 'status' => 1,
  330. )
  331. );
  332. $this->drupalLogin($web_user_3);
  333. $this->assertCurrentUserCanPublish($node3);
  334. $this->assertCurrentUserCanUnpublish($node3);
  335. }
  336. /**
  337. * Test basic publish ability using the publishcontent_test module.
  338. */
  339. public function testBasicPublishCallback() {
  340. $type = 'page';
  341. $this->enablePublishContentForContentType(array($type, 'article'));
  342. $web_user_1 = $this->drupalCreateUser(array(
  343. 'access content',
  344. 'publish any content',
  345. ));
  346. $web_user_2 = $this->drupalCreateUser(array(
  347. 'access content',
  348. 'unpublish any content',
  349. ));
  350. $web_user_3 = $this->drupalCreateUser(array(
  351. 'access content',
  352. 'publish any content',
  353. 'unpublish any content',
  354. ));
  355. $web_user_4 = $this->drupalCreateUser(array(
  356. 'access content',
  357. 'publish any ' . $type . ' content',
  358. ));
  359. $web_user_5 = $this->drupalCreateUser(array(
  360. 'access content',
  361. 'unpublish any ' . $type . ' content',
  362. ));
  363. $web_user_6 = $this->drupalCreateUser(array(
  364. 'access content',
  365. 'publish any ' . $type . ' content',
  366. 'unpublish any ' . $type . ' content',
  367. ));
  368. $web_user_7 = $this->drupalCreateUser(array(
  369. 'access content',
  370. 'publish any article content',
  371. 'unpublish any article content',
  372. ));
  373. $node = $this->drupalCreateNode(
  374. array(
  375. 'type' => $type,
  376. 'uid' => 1,
  377. 'status' => 0,
  378. )
  379. );
  380. $this->drupalLogin($web_user_1);
  381. $this->assertCanPublishFromLinksPage($node, 'Someone with publish any content can publish page node');
  382. $this->assertCannotUnpublishFromLinksPage($node, 'Someone with publish any content cannot unpublish page node');
  383. $this->drupalLogin($web_user_2);
  384. $this->assertCannotPublishFromLinksPage($node, 'Someone with unpublish any content cannot publish page node');
  385. $this->assertCanUnpublishFromLinksPage($node, 'Someone with unpublish any content can unpublish page node');
  386. $this->drupalLogin($web_user_3);
  387. $this->assertCanPublishFromLinksPage($node, 'Someone with publish and unpublish any content can publish page node');
  388. $this->assertCanUnpublishFromLinksPage($node, 'Someone with publish and unpublish any content can unpublish page node');
  389. $this->drupalLogin($web_user_4);
  390. $this->assertCanPublishFromLinksPage($node, 'Someone with publish any page nodes can publish a page node');
  391. $this->assertCannotUnpublishFromLinksPage($node, 'Someone with publish any page nodes cannot unpublish page node');
  392. $this->drupalLogin($web_user_5);
  393. $this->assertCannotPublishFromLinksPage($node, 'Someone with unpublish any page node cannot publish a page node');
  394. $this->assertCanUnpublishFromLinksPage($node, 'Someone with unpublish any page node can unpublish a page node');
  395. $this->drupalLogin($web_user_6);
  396. $this->assertCanPublishFromLinksPage($node, 'Someone with publish and unpublish any page node can publish a page node');
  397. $this->assertCanUnpublishFromLinksPage($node, 'Someone with publish and unpublish any page node can unpublish a page node');
  398. $this->drupalLogin($web_user_7);
  399. $this->assertCannotPublishFromLinksPage($node, 'Someone with publish any article content cannot publish page content');
  400. $this->assertCannotUnpublishFromLinksPage($node, 'Someone with unpublish any article content cannot unpublish page content');
  401. }
  402. }
  403. /**
  404. * Test permissions with the tab method.
  405. */
  406. class PublishContentTabTests extends PublishContentWebTestBase {
  407. /**
  408. * Drupal SimpleTest method: return metadata about the test.
  409. */
  410. public static function getInfo() {
  411. return array(
  412. 'name' => t('Publish Content: Tab Tests'),
  413. 'description' => t('Executes test suite for Publish Content module with the tab method.'),
  414. 'group' => t('Publish Content'),
  415. );
  416. }
  417. /**
  418. * Test setup instructions.
  419. */
  420. public function setUp() {
  421. parent::setUp('publishcontent', 'publishcontent_test');
  422. variable_set('publishcontent_method', PUBLISHCONTENT_METHOD_TABS);
  423. }
  424. /**
  425. * Perform a GET operation on a node.
  426. *
  427. * This will check the response to access some operation via
  428. * the URL of a node. In the case of 'publish' or 'unpublish'
  429. * it will first visit the view of a node so that the relevant
  430. * tabs can be generated.
  431. *
  432. * @param int $nid
  433. * The node nid
  434. * @param string $op
  435. * An operation such as 'view', 'edit', 'publish', 'unpublish'
  436. * @param int $expected_response
  437. * The expexted response code. If the user should not be able to
  438. * see the 'publish' or 'unpublish' tabs, set this to 403, otherwise
  439. * 200.
  440. * @param string $msg
  441. * (optional) An assertion log message.
  442. */
  443. public function assertNodeOperationAccess($nid, $op, $expected_response, $msg = '') {
  444. if (in_array($op, array('publish', 'unpublish'))) {
  445. $tab_link_text = ucfirst($op);
  446. // Visit the edit page first to generate the tab.
  447. $this->drupalGet("node/{$nid}");
  448. $view_response = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
  449. if ($view_response != 200) {
  450. $msg .= t('Could not view the node. Response code: @response', array('@response' => $view_response));
  451. $this->assert($expected_response == $view_response, $msg);
  452. return;
  453. }
  454. // Check the tab exists.
  455. $links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $tab_link_text));
  456. if (!isset($links[0])) {
  457. // No tab.
  458. $msg .= t('Could not find a tab called @tab', array('@tab' => $tab_link_text));
  459. $this->assert($expected_response != 200, $msg);
  460. return;
  461. }
  462. // Now visit the tab.
  463. $this->clickLink($tab_link_text);
  464. $node = node_load($nid);
  465. if ($op == 'publish') {
  466. $this->assertText(_publishcontent_get_message($node->nid, $node->title, TRUE),
  467. 'Publish content message is visible.');
  468. }
  469. else {
  470. $this->assertText(_publishcontent_get_message($node->nid, $node->title, FALSE),
  471. 'Unpublish message is visible.');
  472. }
  473. }
  474. else {
  475. $url = $op == 'view' ? "node/{$nid}" : "node/$nid/$op";
  476. $this->drupalGet($url);
  477. }
  478. $this->assertResponse($expected_response, $msg);
  479. }
  480. }
  481. /**
  482. * Test permissions with the button method.
  483. */
  484. class PublishContentButtonTests extends PublishContentWebTestBase {
  485. /**
  486. * Drupal SimpleTest method: return metadata about the test.
  487. */
  488. public static function getInfo() {
  489. return array(
  490. 'name' => t('Publish Content: Button Tests'),
  491. 'description' => t('Executes test suite for Publish Content module with the button method.'),
  492. 'group' => t('Publish Content'),
  493. );
  494. }
  495. /**
  496. * Test setup instructions.
  497. */
  498. public function setUp() {
  499. parent::setUp('publishcontent', 'publishcontent_test');
  500. variable_set('publishcontent_method', PUBLISHCONTENT_METHOD_BUTTON);
  501. }
  502. /**
  503. * Perform a GET operation on a node.
  504. *
  505. * This will check the response to access some operation via
  506. * the URL of a node. In the case of 'publish' or 'unpublish'
  507. * it will first visit the view of a node so that the relevant
  508. * tabs can be generated.
  509. *
  510. * @param int $nid
  511. * The node nid
  512. * @param string $op
  513. * An operation such as 'view', 'edit', 'publish', 'unpublish'
  514. * @param int $expected_response
  515. * The expexted response code. If the user should not be able to
  516. * see the 'publish' or 'unpublish' tabs, set this to 403, otherwise
  517. * 200.
  518. * @param string $msg
  519. * (optional) An assertion log message.
  520. */
  521. public function assertNodeOperationAccess($nid, $op, $expected_response, $msg = '') {
  522. if (in_array($op, array('publish', 'unpublish'))) {
  523. $button_text = t(ucfirst($op));
  524. // Visit the edit page first to generate the tab.
  525. $this->drupalGet("node/{$nid}/edit");
  526. $view_response = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
  527. if ($view_response != 200) {
  528. $msg .= t('Could not view the node. Response code: @response', array('@response' => $view_response));
  529. $this->assert($expected_response == $view_response, $msg);
  530. return;
  531. }
  532. // Find the button.
  533. $buttons = $this->xpath('//input[@value=:label]', array(':label' => $button_text));
  534. if (!isset($buttons[0])) {
  535. // No button.
  536. $msg .= t('Could not find a button called @button', array('@button' => $button_text));
  537. $this->assert($expected_response != 200, $msg);
  538. return;
  539. }
  540. // Submit the form.
  541. $this->drupalPost("node/{$nid}/edit", array(), $button_text);
  542. }
  543. else {
  544. $url = $op == 'view' ? "node/{$nid}" : "node/$nid/$op";
  545. $this->drupalGet($url);
  546. }
  547. $this->assertResponse($expected_response, $msg);
  548. }
  549. }
  550. /**
  551. * Test permissions with the tab method.
  552. */
  553. class PublishContentCheckboxTests extends PublishContentWebTestBase {
  554. /**
  555. * Drupal SimpleTest method: return metadata about the test.
  556. */
  557. public static function getInfo() {
  558. return array(
  559. 'name' => t('Publish Content: Checkbox Tests'),
  560. 'description' => t('Executes test suite for Publish Content module with the checkbox method.'),
  561. 'group' => t('Publish Content'),
  562. );
  563. }
  564. /**
  565. * Test setup instructions.
  566. */
  567. public function setUp() {
  568. parent::setUp('publishcontent', 'publishcontent_test');
  569. variable_set('publishcontent_method', PUBLISHCONTENT_METHOD_NONE);
  570. }
  571. /**
  572. * Perform a GET operation on a node.
  573. *
  574. * This will check the response to access some operation via
  575. * the URL of a node. In the case of 'publish' or 'unpublish'
  576. * it will first visit the view of a node so that the relevant
  577. * tabs can be generated.
  578. *
  579. * @param int $nid
  580. * The node nid
  581. * @param string $op
  582. * An operation such as 'view', 'edit', 'publish', 'unpublish'
  583. * @param int $expected_response
  584. * The expexted response code. If the user should not be able to
  585. * see the 'publish' or 'unpublish' tabs, set this to 403, otherwise
  586. * 200.
  587. * @param string $msg
  588. * (optional) An assertion log message.
  589. */
  590. public function assertNodeOperationAccess($nid, $op, $expected_response, $msg = '') {
  591. if (in_array($op, array('publish', 'unpublish'))) {
  592. // Visit the edit page first to check for the published checkbox.
  593. $this->drupalGet("node/{$nid}/edit");
  594. $view_response = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
  595. if ($view_response != 200) {
  596. $msg .= t('Could not edit the node. Response code: @response', array('@response' => $view_response));
  597. $this->assert($expected_response == $view_response, $msg);
  598. return;
  599. }
  600. // Check the checkbox exists.
  601. $elements = $this->xpath('//input[@id=:id]', array(':id' => 'edit-status'));
  602. if (!isset($elements[0])) {
  603. // No checkboxes.
  604. $msg .= t('Published status checkbox is not accessible.');
  605. $this->assert($expected_response != 200, $msg);
  606. return;
  607. }
  608. // At this point the response code for checkboxes is irrelevant, if
  609. // they can access the edit page and see the checkbox, they can edit it.
  610. $expected_response = 200;
  611. // Tick or untick the published checkbox and submit the form.
  612. $edit_status = ($op == 'publish');
  613. $this->drupalPost("node/{$nid}/edit", array('status' => $edit_status), t('Save'));
  614. }
  615. else {
  616. $url = $op == 'view' ? "node/{$nid}" : "node/$nid/$op";
  617. $this->drupalGet($url);
  618. }
  619. $this->assertResponse($expected_response, $msg);
  620. }
  621. /**
  622. * Test access to the node add page is working.
  623. */
  624. public function testAccessNodeAdd() {
  625. $type = 'page';
  626. $this->enablePublishContentForContentType($type);
  627. $web_user_1 = $this->drupalCreateUser(array(
  628. 'access content',
  629. 'view own unpublished content',
  630. 'create ' . $type . ' content',
  631. 'publish editable content',
  632. 'unpublish editable content',
  633. ));
  634. $this->drupalLogin($web_user_1);
  635. $this->drupalGet('node/add/' . $type);
  636. $this->assertResponse(200);
  637. $this->assertFieldChecked('edit-status', t('Ensure the publish checkbox is available.'));
  638. }
  639. }