context.conditions.test 18 KB

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