context.test 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. class ContextUnitTest extends DrupalWebTestCase {
  3. protected $profile = 'testing';
  4. public static function getInfo() {
  5. return array(
  6. 'name' => 'API unit tests',
  7. 'description' => 'Sets all possible context types and checks for integrity.',
  8. 'group' => 'Context',
  9. );
  10. }
  11. public function setUp() {
  12. parent::setUp('context');
  13. }
  14. public function test() {
  15. // define possible data types
  16. $set_types = array(
  17. 'bool' => TRUE,
  18. 'int' => 1,
  19. 'string' => 'lorem',
  20. 'array' => array('lorem'),
  21. 'object' => new stdClass(),
  22. );
  23. $id_types = array('int', 'string');
  24. // NAMESPACE
  25. foreach ($set_types as $type => $val) {
  26. $set = context_set($val);
  27. // Test return value of context_set()
  28. if (in_array($type, $id_types)) {
  29. // test set integrity
  30. $this->assertIdentical(true, $set, 'Space set successful.');
  31. // test get integrity
  32. $this->assertIdentical(array(), context_get($val), 'Namespace get successful.');
  33. $this->assertIdentical(true, context_exists($val), 'Namespace exists successful.');
  34. }
  35. else {
  36. $this->assertIdentical(false, $set, 'Prohibited namespace not established.');
  37. }
  38. context_clear();
  39. }
  40. // NAMESPACE+ATTRIBUTE
  41. foreach ($set_types as $type => $val) {
  42. foreach ($set_types as $type2 => $val2) {
  43. // test set integrity
  44. $set = context_set($val, $val2);
  45. if (in_array($type, $id_types)) {
  46. // test set integrity
  47. if ($type2 != 'bool') {
  48. $this->assertIdentical(true, $set, 'Namespace and attribute set successful.');
  49. }
  50. else {
  51. $this->assertIdentical(false, $set);
  52. }
  53. // test get + exists integrity
  54. if (in_array($type2, $id_types)) {
  55. $this->assertIdentical(true, (context_get($val, $val2) == $val2), 'Namespace and attribute get successful.');
  56. $this->assertIdentical(true, context_exists($val, $val2), 'Namespace and attribute exists.');
  57. }
  58. elseif (in_array($type2, array('array', 'object'))) {
  59. $this->assertIdentical(true, (context_get($val) == $val2), 'Namespace and attribute get successful.');
  60. $this->assertIdentical(true, context_exists($val), 'Namespace and attribute exists.');
  61. }
  62. }
  63. }
  64. context_clear();
  65. }
  66. // NAMESPACE+ATTRIBUTE+VALUE, o lord
  67. foreach ($set_types as $type => $val) {
  68. foreach ($set_types as $type2 => $val2) {
  69. foreach ($set_types as $type3 => $val3) {
  70. $set = context_set($val, $val2, $val3);
  71. if (in_array($type, $id_types)) {
  72. if (in_array($type2, $id_types)) {
  73. $this->assertIdentical(true, (context_get($val, $val2, $val3) == $val3), 'Namespace, attribute and value get successful.');
  74. $this->assertIdentical(true, context_exists($val, $val2, $val3), 'Namespace, attribute and value exists.');
  75. }
  76. }
  77. context_clear();
  78. }
  79. }
  80. }
  81. }
  82. }