context.conditions.test

  1. nittany7 modules/contrib/context/tests/context.conditions.test
  2. cis7 modules/contrib/context/tests/context.conditions.test
  3. mooc7 modules/contrib/context/tests/context.conditions.test

Classes

File

modules/contrib/context/tests/context.conditions.test
View source
  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. // Set up Spanish as second language.
  178. $this->drupalPost('admin/config/regional/language/add', array('langcode' => 'es'), t('Add language'));
  179. $this->drupalPost('admin/config/regional/language/configure', array('language[enabled][locale-url]' => 1), t('Save settings'));
  180. }
  181. function test() {
  182. ctools_include('export');
  183. $context = ctools_export_new_object('context');
  184. $context->name = 'testcontext';
  185. $context->conditions = array('language' => array('values' => array('es')));
  186. $context->reactions = array('debug' => array('debug' => TRUE));
  187. $saved = context_save($context);
  188. $this->assertTrue($saved, "Context 'testcontext' saved.");
  189. $this->drupalGet('node');
  190. $this->assertNoText('Active context: testcontext');
  191. $this->drupalGet('es/node');
  192. $this->assertText('Active context: testcontext');
  193. // Cleanup
  194. context_delete($context);
  195. }
  196. }
  197. class ContextConditionSitewideTest extends DrupalWebTestCase {
  198. protected $profile = 'testing';
  199. public static function getInfo() {
  200. return array(
  201. 'name' => 'Condition: sitewide',
  202. 'description' => 'Test sitewide condition.',
  203. 'group' => 'Context',
  204. );
  205. }
  206. function setUp() {
  207. parent::setUp('context', 'ctools');
  208. $admin_user = $this->drupalCreateUser(array('administer site configuration'));
  209. $this->drupalLogin($admin_user);
  210. }
  211. function test() {
  212. ctools_include('export');
  213. $context = ctools_export_new_object('context');
  214. $context->name = 'testcontext';
  215. $context->conditions = array('sitewide' => array('values' => array(1)));
  216. $context->reactions = array('debug' => array('debug' => TRUE));
  217. $saved = context_save($context);
  218. $this->assertTrue($saved, "Context 'testcontext' saved.");
  219. $this->drupalGet('node');
  220. $this->assertText('Active context: testcontext');
  221. // Cleanup
  222. context_delete($context);
  223. }
  224. }
  225. class ContextConditionPathTest extends DrupalWebTestCase {
  226. protected $profile = 'testing';
  227. public static function getInfo() {
  228. return array(
  229. 'name' => 'Condition: path',
  230. 'description' => 'Test path condition.',
  231. 'group' => 'Context',
  232. );
  233. }
  234. function setUp() {
  235. parent::setUp('context', 'ctools', 'path');
  236. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes'));
  237. $this->drupalLogin($admin_user);
  238. }
  239. function test() {
  240. ctools_include('export');
  241. $context = ctools_export_new_object('context');
  242. $context->name = 'testcontext';
  243. $context->conditions = array('path' => array('values' => array('admin', 'node/*')));
  244. $context->reactions = array('debug' => array('debug' => TRUE));
  245. $saved = context_save($context);
  246. $this->assertTrue($saved, "Context 'testcontext' saved.");
  247. $this->drupalGet('admin');
  248. $this->assertText('Active context: testcontext');
  249. $node = $this->drupalCreateNode();
  250. $this->drupalGet("node/{$node->nid}");
  251. $this->assertText('Active context: testcontext');
  252. $this->drupalGet('node');
  253. $this->assertNoText('Active context: testcontext');
  254. // Cleanup
  255. context_delete($context);
  256. // @TODO: Test with path alias
  257. // @TODO: Test with language prefixes
  258. }
  259. }
  260. class ContextConditionContextTest extends DrupalWebTestCase {
  261. protected $profile = 'testing';
  262. public static function getInfo() {
  263. return array(
  264. 'name' => 'Condition: context',
  265. 'description' => 'Test context condition.',
  266. 'group' => 'Context',
  267. );
  268. }
  269. function setUp() {
  270. parent::setUp('context', 'ctools');
  271. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes'));
  272. $this->drupalLogin($admin_user);
  273. }
  274. function test() {
  275. ctools_include('export');
  276. $context = ctools_export_new_object('context');
  277. $context->name = 'testcontext';
  278. $context->conditions = array('path' => array('values' => array('admin')));
  279. $context->reactions = array('debug' => array('debug' => TRUE));
  280. $saved = context_save($context);
  281. $this->assertTrue($saved, "Context 'testcontext' saved.");
  282. $subcontext = ctools_export_new_object('context');
  283. $subcontext->name = 'subcontext';
  284. $subcontext->conditions = array('context' => array('values' => array('testcontext')));
  285. $subcontext->reactions = array('debug' => array('debug' => TRUE));
  286. $saved = context_save($subcontext);
  287. $this->assertTrue($saved, "Context 'subcontext' saved.");
  288. $this->drupalGet('admin');
  289. $this->assertText('Active context: testcontext');
  290. $this->assertText('Active context: subcontext');
  291. // Cleanup
  292. context_delete($context);
  293. // @TODO: Test exclusion
  294. }
  295. }
  296. class ContextConditionNodeTest extends DrupalWebTestCase {
  297. protected $profile = 'testing';
  298. public static function getInfo() {
  299. return array(
  300. 'name' => 'Condition: node',
  301. 'description' => 'Test node condition.',
  302. 'group' => 'Context',
  303. );
  304. }
  305. function setUp() {
  306. parent::setUp('context', 'ctools', 'blog', 'book');
  307. $admin_user = $this->drupalCreateUser(array(
  308. 'administer site configuration',
  309. 'administer nodes',
  310. 'create blog content',
  311. 'create book content'
  312. ));
  313. $this->drupalLogin($admin_user);
  314. }
  315. function test() {
  316. ctools_include('export');
  317. $context = ctools_export_new_object('context');
  318. $context->name = 'testcontext';
  319. $context->conditions = array('node' => array('values' => array('blog')));
  320. $context->reactions = array('debug' => array('debug' => TRUE));
  321. $saved = context_save($context);
  322. $this->assertTrue($saved, "Context 'testcontext' saved.");
  323. $this->drupalGet("node/add/blog");
  324. $this->assertNoText('Active context: testcontext');
  325. $this->drupalGet("node/add/book");
  326. $this->assertNoText('Active context: testcontext');
  327. $node = $this->drupalCreateNode(array('type' => 'blog'));
  328. $this->drupalGet("node/{$node->nid}");
  329. $this->assertText('Active context: testcontext');
  330. $node = $this->drupalCreateNode(array('type' => 'book'));
  331. $this->drupalGet("node/{$node->nid}");
  332. $this->assertNoText('Active context: testcontext');
  333. $context->conditions['node']['options']['node_form'] = 1;
  334. $saved = context_save($context);
  335. $this->assertTrue($saved, "Context 'testcontext' saved.");
  336. $this->drupalGet("node/add/blog");
  337. $this->assertText('Active context: testcontext');
  338. $this->drupalGet("node/add/book");
  339. $this->assertNoText('Active context: testcontext');
  340. // Cleanup
  341. context_delete($context);
  342. }
  343. }
  344. class ContextConditionMenuTest extends DrupalWebTestCase {
  345. protected $profile = 'testing';
  346. public static function getInfo() {
  347. return array(
  348. 'name' => 'Condition: menu',
  349. 'description' => 'Test menu condition.',
  350. 'group' => 'Context',
  351. );
  352. }
  353. function setUp() {
  354. parent::setUp('context', 'ctools', 'blog', 'menu');
  355. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes', 'create blog content'));
  356. $this->drupalLogin($admin_user);
  357. }
  358. function test() {
  359. ctools_include('export');
  360. $context = ctools_export_new_object('context');
  361. $context->name = 'testcontext';
  362. $context->conditions = array('menu' => array('values' => array('node/add')));
  363. $context->reactions = array('debug' => array('debug' => TRUE));
  364. $saved = context_save($context);
  365. $this->assertTrue($saved, "Context 'testcontext' saved.");
  366. $this->drupalGet("node/add/blog");
  367. $this->assertText('Active context: testcontext');
  368. $this->drupalGet("node/add");
  369. $this->assertText('Active context: testcontext');
  370. $this->drupalGet("node");
  371. $this->assertNoText('Active context: testcontext');
  372. // Cleanup
  373. context_delete($context);
  374. }
  375. }
  376. class ContextConditionBookTest extends DrupalWebTestCase {
  377. protected $profile = 'testing';
  378. public static function getInfo() {
  379. return array(
  380. 'name' => 'Condition: book',
  381. 'description' => 'Test book condition.',
  382. 'group' => 'Context',
  383. );
  384. }
  385. function setUp() {
  386. parent::setUp('context', 'ctools', 'book', 'menu');
  387. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes'));
  388. $this->drupalLogin($admin_user);
  389. }
  390. function test() {
  391. $book = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => 'new')));
  392. $child = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => $book->nid)));
  393. $dummy = $this->drupalCreateNode(array('type' => 'book'));
  394. ctools_include('export');
  395. $context = ctools_export_new_object('context');
  396. $context->name = 'testcontext';
  397. $context->conditions = array('book' => array('values' => array(book_menu_name($book->book['bid']))));
  398. $context->reactions = array('debug' => array('debug' => TRUE));
  399. $saved = context_save($context);
  400. $this->assertTrue($saved, "Context 'testcontext' saved.");
  401. $this->drupalGet("node/{$book->nid}");
  402. $this->assertText('Active context: testcontext');
  403. $this->drupalGet("node/{$child->nid}");
  404. $this->assertText('Active context: testcontext');
  405. $this->drupalGet("node/{$dummy->nid}");
  406. $this->assertNoText('Active context: testcontext');
  407. // Cleanup
  408. context_delete($context);
  409. }
  410. }
  411. class ContextConditionBookroot extends DrupalWebTestCase {
  412. protected $profile = 'testing';
  413. public static function getInfo() {
  414. return array(
  415. 'name' => 'Condition: bookroot',
  416. 'description' => 'Test bookroot condition.',
  417. 'group' => 'Context',
  418. );
  419. }
  420. function setUp() {
  421. parent::setUp('context', 'ctools', 'book', 'menu');
  422. $admin_user = $this->drupalCreateUser(array(
  423. 'administer site configuration',
  424. 'administer nodes',
  425. 'create book content',
  426. 'edit any book content',
  427. ));
  428. $this->drupalLogin($admin_user);
  429. variable_set('book_allowed_types', array('book', 'page'));
  430. }
  431. function test() {
  432. $book = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => 'new')));
  433. $child = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => $book->nid)));
  434. $dummy = $this->drupalCreateNode(array('type' => 'page', 'book' => array('bid' => 'new')));
  435. $dummy_child = $this->drupalCreateNode(array('type' => 'page', 'book' => array('bid' => $dummy->nid)));
  436. ctools_include('export');
  437. $context = ctools_export_new_object('context');
  438. $context->name = 'testcontext';
  439. $context->conditions = array('bookroot' => array('values' => array('book')));
  440. $context->reactions = array('debug' => array('debug' => TRUE));
  441. $saved = context_save($context);
  442. $this->assertTrue($saved, "Context 'testcontext' saved.");
  443. $this->drupalGet("node/{$book->nid}");
  444. $this->assertText('Active context: testcontext');
  445. $this->drupalGet("node/{$child->nid}");
  446. $this->assertText('Active context: testcontext');
  447. $this->drupalGet("node/{$dummy->nid}");
  448. $this->assertNoText('Active context: testcontext');
  449. $this->drupalGet("node/{$dummy_child->nid}");
  450. $this->assertNoText('Active context: testcontext');
  451. $this->drupalGet("node/{$book->nid}/edit");
  452. $this->assertNoText('Active context: testcontext');
  453. $context->conditions['bookroot']['options']['node_form'] = 1;
  454. $saved = context_save($context);
  455. $this->assertTrue($saved, "Context 'testcontext' saved.");
  456. $this->drupalGet("node/{$book->nid}/edit");
  457. $this->assertText('Active context: testcontext');
  458. // Cleanup
  459. context_delete($context);
  460. }
  461. }