context.conditions.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. class ContextConditionUserTest extends DrupalWebTestCase {
  3. protected $profile = 'testing';
  4. public static function getInfo() {
  5. return array(
  6. 'name' => 'Condition: user',
  7. 'description' => 'Test user condition.',
  8. 'group' => 'Context',
  9. );
  10. }
  11. function setUp() {
  12. parent::setUp('context', 'ctools');
  13. $this->user1 = $this->drupalCreateUser(array('access content', 'administer site configuration'));
  14. $this->user2 = $this->drupalCreateUser(array('access content'));
  15. // The role name is not reliably put on the user object. Retrive from
  16. // user_roles().
  17. $role = '';
  18. foreach (array_keys($this->user1->roles) as $rid) {
  19. if ($rid !== DRUPAL_AUTHENTICATED_RID) {
  20. $role = user_role_load($rid)->name;
  21. break;
  22. }
  23. }
  24. // Create test context.
  25. ctools_include('export');
  26. $this->context = ctools_export_new_object('context');
  27. $this->context->name = 'testcontext';
  28. $this->context->conditions = array('user' => array('values' => array($role)));
  29. $this->context->reactions = array('debug' => array('debug' => TRUE));
  30. $saved = context_save($this->context);
  31. $this->assertTrue($saved, "Context 'testcontext' saved.");
  32. }
  33. function test() {
  34. // User 1 triggers the context.
  35. $this->drupalLogin($this->user1);
  36. $this->drupalGet('node');
  37. $this->assertText('Active context: testcontext');
  38. // User 2 does not.
  39. $this->drupalLogin($this->user2);
  40. $this->drupalGet('node');
  41. $this->assertNoText('Active context: testcontext');
  42. }
  43. }
  44. class ContextConditionUserPageTest extends DrupalWebTestCase {
  45. protected $profile = 'testing';
  46. public static function getInfo() {
  47. return array(
  48. 'name' => 'Condition: user page',
  49. 'description' => 'Test user page condition.',
  50. 'group' => 'Context',
  51. );
  52. }
  53. function setUp() {
  54. parent::setUp('context', 'ctools');
  55. $this->user1 = $this->drupalCreateUser(array('access user profiles', 'access content', 'administer site configuration'));
  56. $this->user2 = $this->drupalCreateUser(array('access user profiles', 'access content'));
  57. // Create test context.
  58. ctools_include('export');
  59. $this->context = ctools_export_new_object('context');
  60. $this->context->name = 'testcontext';
  61. $this->context->conditions = array('user_page' => array('values' => array('view' => 'view'), 'options' => array('mode' => 'all')));
  62. $this->context->reactions = array('debug' => array('debug' => TRUE));
  63. $saved = context_save($this->context);
  64. $this->assertTrue($saved, "Context 'testcontext' saved.");
  65. }
  66. function test() {
  67. // Viewing any user profile triggers context.
  68. $this->drupalLogin($this->user1);
  69. $this->drupalGet("user/{$this->user1->uid}");
  70. $this->assertText('Active context: testcontext');
  71. $this->drupalGet("user/{$this->user2->uid}");
  72. $this->assertText('Active context: testcontext');
  73. // User form does not.
  74. $this->drupalGet("user/{$this->user1->uid}/edit");
  75. $this->assertNoText('Active context: testcontext');
  76. // Test current user mode
  77. $this->context->conditions['user_page']['options']['mode'] = 'current';
  78. $saved = context_save($this->context);
  79. $this->assertTrue($saved, "Context 'testcontext' saved.");
  80. $this->drupalGet("user/{$this->user1->uid}");
  81. $this->assertText('Active context: testcontext');
  82. $this->drupalGet("user/{$this->user2->uid}");
  83. $this->assertNoText('Active context: testcontext');
  84. // Test other user mode
  85. $this->context->conditions['user_page']['options']['mode'] = 'other';
  86. $saved = context_save($this->context);
  87. $this->assertTrue($saved, "Context 'testcontext' saved.");
  88. $this->drupalGet("user/{$this->user1->uid}");
  89. $this->assertNoText('Active context: testcontext');
  90. $this->drupalGet("user/{$this->user2->uid}");
  91. $this->assertText('Active context: testcontext');
  92. }
  93. }
  94. class ContextConditionNodeTaxonomyTest extends DrupalWebTestCase {
  95. // We want the default taxonomy and content types created
  96. protected $profile = 'standard';
  97. public static function getInfo() {
  98. return array(
  99. 'name' => 'Condition: taxonomy',
  100. 'description' => 'Test taxonomy condition.',
  101. 'group' => 'Context',
  102. );
  103. }
  104. function setUp() {
  105. parent::setUp('context', 'ctools', 'taxonomy');
  106. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'create article content'));
  107. $this->drupalLogin($admin_user);
  108. // Create test terms.
  109. $this->vocab = taxonomy_vocabulary_machine_name_load('tags');
  110. $this->terms = array();
  111. $this->terms['apples'] = (object)array('name' => 'apples', 'vid' => $this->vocab->vid);
  112. $this->terms['oranges'] = (object)array('name' => 'oranges', 'vid' => $this->vocab->vid);
  113. taxonomy_term_save($this->terms['apples']);
  114. taxonomy_term_save($this->terms['oranges']);
  115. // Create test context.
  116. ctools_include('export');
  117. $this->context = ctools_export_new_object('context');
  118. $this->context->name = 'testcontext';
  119. $this->context->conditions = array('node_taxonomy' => array('values' => array($this->terms['apples']->tid)));
  120. $this->context->reactions = array('debug' => array('debug' => TRUE));
  121. $saved = context_save($this->context);
  122. $this->assertTrue($saved, "Context 'testcontext' saved.");
  123. }
  124. function test() {
  125. // Apples does trigger the context.
  126. $edit = array(
  127. 'title' => 'Apples',
  128. 'field_tags[und]' => $this->terms['apples']->name
  129. );
  130. $this->drupalPost('node/add/article', $edit, t('Save'));
  131. $node = $this->drupalGetNodeByTitle($edit['title']);
  132. $this->drupalGet('node/' . $node->nid);
  133. $this->assertText('Active context: testcontext');
  134. // Oranges does not trigger the context.
  135. $edit = array(
  136. 'title' => 'Oranges',
  137. 'field_tags[und]' => $this->terms['oranges']->name
  138. );
  139. $this->drupalPost('node/add/article', $edit, t('Save'));
  140. $node = $this->drupalGetNodeByTitle($edit['title']);
  141. $this->drupalGet('node/' . $node->nid);
  142. $this->assertNoText('Active context: testcontext');
  143. }
  144. }
  145. class ContextConditionLanguageTest extends DrupalWebTestCase {
  146. protected $profile = 'testing';
  147. public static function getInfo() {
  148. return array(
  149. 'name' => 'Condition: language',
  150. 'description' => 'Test language condition.',
  151. 'group' => 'Context',
  152. );
  153. }
  154. function setUp() {
  155. parent::setUp('context', 'ctools', 'locale');
  156. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages'));
  157. $this->drupalLogin($admin_user);
  158. $this->drupalPost('admin/config/development/performance', array(), t('Clear all caches'));
  159. // Set up Spanish as second language.
  160. $this->drupalPost('admin/config/regional/language/add', array('langcode' => 'es'), t('Add language'));
  161. $this->drupalPost('admin/config/regional/language/configure', array('language[enabled][locale-url]' => 1), t('Save settings'));
  162. }
  163. function test() {
  164. ctools_include('export');
  165. $context = ctools_export_new_object('context');
  166. $context->name = 'testcontext';
  167. $context->conditions = array('language' => array('values' => array('es')));
  168. $context->reactions = array('debug' => array('debug' => TRUE));
  169. $saved = context_save($context);
  170. $this->assertTrue($saved, "Context 'testcontext' saved.");
  171. $this->drupalGet('node');
  172. $this->assertNoText('Active context: testcontext');
  173. $this->drupalGet('es/node');
  174. $this->assertText('Active context: testcontext');
  175. // Cleanup
  176. context_delete($context);
  177. }
  178. }
  179. class ContextConditionSitewideTest extends DrupalWebTestCase {
  180. protected $profile = 'testing';
  181. public static function getInfo() {
  182. return array(
  183. 'name' => 'Condition: sitewide',
  184. 'description' => 'Test sitewide condition.',
  185. 'group' => 'Context',
  186. );
  187. }
  188. function setUp() {
  189. parent::setUp('context', 'ctools');
  190. $admin_user = $this->drupalCreateUser(array('administer site configuration'));
  191. $this->drupalLogin($admin_user);
  192. }
  193. function test() {
  194. ctools_include('export');
  195. $context = ctools_export_new_object('context');
  196. $context->name = 'testcontext';
  197. $context->conditions = array('sitewide' => array('values' => array(1)));
  198. $context->reactions = array('debug' => array('debug' => TRUE));
  199. $saved = context_save($context);
  200. $this->assertTrue($saved, "Context 'testcontext' saved.");
  201. $this->drupalGet('node');
  202. $this->assertText('Active context: testcontext');
  203. // Cleanup
  204. context_delete($context);
  205. }
  206. }
  207. class ContextConditionPathTest extends DrupalWebTestCase {
  208. protected $profile = 'testing';
  209. public static function getInfo() {
  210. return array(
  211. 'name' => 'Condition: path',
  212. 'description' => 'Test path condition.',
  213. 'group' => 'Context',
  214. );
  215. }
  216. function setUp() {
  217. parent::setUp('context', 'ctools', 'path');
  218. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes'));
  219. $this->drupalLogin($admin_user);
  220. }
  221. function test() {
  222. ctools_include('export');
  223. $context = ctools_export_new_object('context');
  224. $context->name = 'testcontext';
  225. $context->conditions = array('path' => array('values' => array('admin', 'node/*')));
  226. $context->reactions = array('debug' => array('debug' => TRUE));
  227. $saved = context_save($context);
  228. $this->assertTrue($saved, "Context 'testcontext' saved.");
  229. $this->drupalGet('admin');
  230. $this->assertText('Active context: testcontext');
  231. $node = $this->drupalCreateNode();
  232. $this->drupalGet("node/{$node->nid}");
  233. $this->assertText('Active context: testcontext');
  234. $this->drupalGet('node');
  235. $this->assertNoText('Active context: testcontext');
  236. // Cleanup
  237. context_delete($context);
  238. // @TODO: Test with path alias
  239. // @TODO: Test with language prefixes
  240. }
  241. }
  242. class ContextConditionContextTest extends DrupalWebTestCase {
  243. protected $profile = 'testing';
  244. public static function getInfo() {
  245. return array(
  246. 'name' => 'Condition: context',
  247. 'description' => 'Test context condition.',
  248. 'group' => 'Context',
  249. );
  250. }
  251. function setUp() {
  252. parent::setUp('context', 'ctools');
  253. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes'));
  254. $this->drupalLogin($admin_user);
  255. }
  256. function test() {
  257. ctools_include('export');
  258. $context = ctools_export_new_object('context');
  259. $context->name = 'testcontext';
  260. $context->conditions = array('path' => array('values' => array('admin')));
  261. $context->reactions = array('debug' => array('debug' => TRUE));
  262. $saved = context_save($context);
  263. $this->assertTrue($saved, "Context 'testcontext' saved.");
  264. $subcontext = ctools_export_new_object('context');
  265. $subcontext->name = 'subcontext';
  266. $subcontext->conditions = array('context' => array('values' => array('testcontext')));
  267. $subcontext->reactions = array('debug' => array('debug' => TRUE));
  268. $saved = context_save($subcontext);
  269. $this->assertTrue($saved, "Context 'subcontext' saved.");
  270. $this->drupalGet('admin');
  271. $this->assertText('Active context: testcontext');
  272. $this->assertText('Active context: subcontext');
  273. // Cleanup
  274. context_delete($context);
  275. // @TODO: Test exclusion
  276. }
  277. }
  278. class ContextConditionNodeTest extends DrupalWebTestCase {
  279. protected $profile = 'testing';
  280. public static function getInfo() {
  281. return array(
  282. 'name' => 'Condition: node',
  283. 'description' => 'Test node condition.',
  284. 'group' => 'Context',
  285. );
  286. }
  287. function setUp() {
  288. parent::setUp('context', 'ctools', 'blog', 'book');
  289. $admin_user = $this->drupalCreateUser(array(
  290. 'administer site configuration',
  291. 'administer nodes',
  292. 'create blog content',
  293. 'create book content'
  294. ));
  295. $this->drupalLogin($admin_user);
  296. }
  297. function test() {
  298. ctools_include('export');
  299. $context = ctools_export_new_object('context');
  300. $context->name = 'testcontext';
  301. $context->conditions = array('node' => array('values' => array('blog')));
  302. $context->reactions = array('debug' => array('debug' => TRUE));
  303. $saved = context_save($context);
  304. $this->assertTrue($saved, "Context 'testcontext' saved.");
  305. $this->drupalGet("node/add/blog");
  306. $this->assertNoText('Active context: testcontext');
  307. $this->drupalGet("node/add/book");
  308. $this->assertNoText('Active context: testcontext');
  309. $node = $this->drupalCreateNode(array('type' => 'blog'));
  310. $this->drupalGet("node/{$node->nid}");
  311. $this->assertText('Active context: testcontext');
  312. $node = $this->drupalCreateNode(array('type' => 'book'));
  313. $this->drupalGet("node/{$node->nid}");
  314. $this->assertNoText('Active context: testcontext');
  315. $context->conditions['node']['options']['node_form'] = 1;
  316. $saved = context_save($context);
  317. $this->assertTrue($saved, "Context 'testcontext' saved.");
  318. $this->drupalGet("node/add/blog");
  319. $this->assertText('Active context: testcontext');
  320. $this->drupalGet("node/add/book");
  321. $this->assertNoText('Active context: testcontext');
  322. // Cleanup
  323. context_delete($context);
  324. }
  325. }
  326. class ContextConditionMenuTest extends DrupalWebTestCase {
  327. protected $profile = 'testing';
  328. public static function getInfo() {
  329. return array(
  330. 'name' => 'Condition: menu',
  331. 'description' => 'Test menu condition.',
  332. 'group' => 'Context',
  333. );
  334. }
  335. function setUp() {
  336. parent::setUp('context', 'ctools', 'blog', 'menu');
  337. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes', 'create blog content'));
  338. $this->drupalLogin($admin_user);
  339. }
  340. function test() {
  341. ctools_include('export');
  342. $context = ctools_export_new_object('context');
  343. $context->name = 'testcontext';
  344. $context->conditions = array('menu' => array('values' => array('node/add')));
  345. $context->reactions = array('debug' => array('debug' => TRUE));
  346. $saved = context_save($context);
  347. $this->assertTrue($saved, "Context 'testcontext' saved.");
  348. $this->drupalGet("node/add/blog");
  349. $this->assertText('Active context: testcontext');
  350. $this->drupalGet("node/add");
  351. $this->assertText('Active context: testcontext');
  352. $this->drupalGet("node");
  353. $this->assertNoText('Active context: testcontext');
  354. // Cleanup
  355. context_delete($context);
  356. }
  357. }
  358. class ContextConditionBookTest extends DrupalWebTestCase {
  359. protected $profile = 'testing';
  360. public static function getInfo() {
  361. return array(
  362. 'name' => 'Condition: book',
  363. 'description' => 'Test book condition.',
  364. 'group' => 'Context',
  365. );
  366. }
  367. function setUp() {
  368. parent::setUp('context', 'ctools', 'book', 'menu');
  369. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes'));
  370. $this->drupalLogin($admin_user);
  371. }
  372. function test() {
  373. $book = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => 'new')));
  374. $child = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => $book->nid)));
  375. $dummy = $this->drupalCreateNode(array('type' => 'book'));
  376. ctools_include('export');
  377. $context = ctools_export_new_object('context');
  378. $context->name = 'testcontext';
  379. $context->conditions = array('book' => array('values' => array(book_menu_name($book->book['bid']))));
  380. $context->reactions = array('debug' => array('debug' => TRUE));
  381. $saved = context_save($context);
  382. $this->assertTrue($saved, "Context 'testcontext' saved.");
  383. $this->drupalGet("node/{$book->nid}");
  384. $this->assertText('Active context: testcontext');
  385. $this->drupalGet("node/{$child->nid}");
  386. $this->assertText('Active context: testcontext');
  387. $this->drupalGet("node/{$dummy->nid}");
  388. $this->assertNoText('Active context: testcontext');
  389. // Cleanup
  390. context_delete($context);
  391. }
  392. }
  393. class ContextConditionBookroot extends DrupalWebTestCase {
  394. protected $profile = 'testing';
  395. public static function getInfo() {
  396. return array(
  397. 'name' => 'Condition: bookroot',
  398. 'description' => 'Test bookroot condition.',
  399. 'group' => 'Context',
  400. );
  401. }
  402. function setUp() {
  403. parent::setUp('context', 'ctools', 'book', 'menu');
  404. $admin_user = $this->drupalCreateUser(array(
  405. 'administer site configuration',
  406. 'administer nodes',
  407. 'create book content',
  408. 'edit any book content',
  409. ));
  410. $this->drupalLogin($admin_user);
  411. variable_set('book_allowed_types', array('book', 'page'));
  412. }
  413. function test() {
  414. $book = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => 'new')));
  415. $child = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => $book->nid)));
  416. $dummy = $this->drupalCreateNode(array('type' => 'page', 'book' => array('bid' => 'new')));
  417. $dummy_child = $this->drupalCreateNode(array('type' => 'page', 'book' => array('bid' => $dummy->nid)));
  418. ctools_include('export');
  419. $context = ctools_export_new_object('context');
  420. $context->name = 'testcontext';
  421. $context->conditions = array('bookroot' => array('values' => array('book')));
  422. $context->reactions = array('debug' => array('debug' => TRUE));
  423. $saved = context_save($context);
  424. $this->assertTrue($saved, "Context 'testcontext' saved.");
  425. $this->drupalGet("node/{$book->nid}");
  426. $this->assertText('Active context: testcontext');
  427. $this->drupalGet("node/{$child->nid}");
  428. $this->assertText('Active context: testcontext');
  429. $this->drupalGet("node/{$dummy->nid}");
  430. $this->assertNoText('Active context: testcontext');
  431. $this->drupalGet("node/{$dummy_child->nid}");
  432. $this->assertNoText('Active context: testcontext');
  433. $this->drupalGet("node/{$book->nid}/edit");
  434. $this->assertNoText('Active context: testcontext');
  435. $context->conditions['bookroot']['options']['node_form'] = 1;
  436. $saved = context_save($context);
  437. $this->assertTrue($saved, "Context 'testcontext' saved.");
  438. $this->drupalGet("node/{$book->nid}/edit");
  439. $this->assertText('Active context: testcontext');
  440. // Cleanup
  441. context_delete($context);
  442. }
  443. }