ElggBatchTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * test \ElggBatch
  4. *
  5. */
  6. class ElggBatchTest extends \ElggCoreUnitTest {
  7. // see https://github.com/elgg/elgg/issues/4288
  8. public function testElggBatchIncOffset() {
  9. // normal increment
  10. $options = array(
  11. 'offset' => 0,
  12. 'limit' => 11
  13. );
  14. $batch = new \ElggBatch(array('\ElggBatchTest', 'elgg_batch_callback_test'), $options,
  15. null, 5);
  16. $j = 0;
  17. foreach ($batch as $e) {
  18. $offset = floor($j / 5) * 5;
  19. $this->assertEqual($offset, $e['offset']);
  20. $this->assertEqual($j + 1, $e['index']);
  21. $j++;
  22. }
  23. $this->assertEqual(11, $j);
  24. // no increment, 0 start
  25. \ElggBatchTest::elgg_batch_callback_test(array(), true);
  26. $options = array(
  27. 'offset' => 0,
  28. 'limit' => 11
  29. );
  30. $batch = new \ElggBatch(array('\ElggBatchTest', 'elgg_batch_callback_test'), $options,
  31. null, 5);
  32. $batch->setIncrementOffset(false);
  33. $j = 0;
  34. foreach ($batch as $e) {
  35. $this->assertEqual(0, $e['offset']);
  36. // should always be the same 5
  37. $this->assertEqual($e['index'], $j + 1 - (floor($j / 5) * 5));
  38. $j++;
  39. }
  40. $this->assertEqual(11, $j);
  41. // no increment, 3 start
  42. \ElggBatchTest::elgg_batch_callback_test(array(), true);
  43. $options = array(
  44. 'offset' => 3,
  45. 'limit' => 11
  46. );
  47. $batch = new \ElggBatch(array('\ElggBatchTest', 'elgg_batch_callback_test'), $options,
  48. null, 5);
  49. $batch->setIncrementOffset(false);
  50. $j = 0;
  51. foreach ($batch as $e) {
  52. $this->assertEqual(3, $e['offset']);
  53. // same 5 results
  54. $this->assertEqual($e['index'], $j + 4 - (floor($j / 5) * 5));
  55. $j++;
  56. }
  57. $this->assertEqual(11, $j);
  58. }
  59. public function testElggBatchReadHandlesBrokenEntities() {
  60. $num_test_entities = 8;
  61. $guids = array();
  62. for ($i = $num_test_entities; $i > 0; $i--) {
  63. $entity = new \ElggObject();
  64. $entity->type = 'object';
  65. $entity->subtype = 'test_5357_subtype';
  66. $entity->access_id = ACCESS_PUBLIC;
  67. $entity->save();
  68. $guids[] = $entity->guid;
  69. _elgg_invalidate_cache_for_entity($entity->guid);
  70. }
  71. // break entities such that the first fetch has one incomplete
  72. // and the second and third fetches have only incompletes!
  73. $db_prefix = elgg_get_config('dbprefix');
  74. delete_data("
  75. DELETE FROM {$db_prefix}objects_entity
  76. WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]}, {$guids[4]}, {$guids[5]})
  77. ");
  78. $options = array(
  79. 'type' => 'object',
  80. 'subtype' => 'test_5357_subtype',
  81. 'order_by' => 'e.guid',
  82. );
  83. $entities_visited = array();
  84. $batch = new \ElggBatch('elgg_get_entities', $options, null, 2);
  85. /* @var \ElggEntity[] $batch */
  86. foreach ($batch as $entity) {
  87. $entities_visited[] = $entity->guid;
  88. }
  89. // The broken entities should not have been visited
  90. $this->assertEqual($entities_visited, array($guids[0], $guids[6], $guids[7]));
  91. // cleanup (including leftovers from previous tests)
  92. $entity_rows = elgg_get_entities(array_merge($options, array(
  93. 'callback' => '',
  94. 'limit' => false,
  95. )));
  96. $guids = array();
  97. foreach ($entity_rows as $row) {
  98. $guids[] = $row->guid;
  99. }
  100. delete_data("DELETE FROM {$db_prefix}entities WHERE guid IN (" . implode(',', $guids) . ")");
  101. delete_data("DELETE FROM {$db_prefix}objects_entity WHERE guid IN (" . implode(',', $guids) . ")");
  102. remove_subtype('object', 'test_5357_subtype');
  103. }
  104. public function testElggBatchDeleteHandlesBrokenEntities() {
  105. $num_test_entities = 8;
  106. $guids = array();
  107. for ($i = $num_test_entities; $i > 0; $i--) {
  108. $entity = new \ElggObject();
  109. $entity->type = 'object';
  110. $entity->subtype = 'test_5357_subtype';
  111. $entity->access_id = ACCESS_PUBLIC;
  112. $entity->save();
  113. $guids[] = $entity->guid;
  114. _elgg_invalidate_cache_for_entity($entity->guid);
  115. }
  116. // break entities such that the first fetch has one incomplete
  117. // and the second and third fetches have only incompletes!
  118. $db_prefix = elgg_get_config('dbprefix');
  119. delete_data("
  120. DELETE FROM {$db_prefix}objects_entity
  121. WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]}, {$guids[4]}, {$guids[5]})
  122. ");
  123. $options = array(
  124. 'type' => 'object',
  125. 'subtype' => 'test_5357_subtype',
  126. 'order_by' => 'e.guid',
  127. );
  128. $entities_visited = array();
  129. $batch = new \ElggBatch('elgg_get_entities', $options, null, 2, false);
  130. /* @var \ElggEntity[] $batch */
  131. foreach ($batch as $entity) {
  132. $entities_visited[] = $entity->guid;
  133. $entity->delete();
  134. }
  135. // The broken entities should not have been visited
  136. $this->assertEqual($entities_visited, array($guids[0], $guids[6], $guids[7]));
  137. // cleanup (including leftovers from previous tests)
  138. $entity_rows = elgg_get_entities(array_merge($options, array(
  139. 'callback' => '',
  140. 'limit' => false,
  141. )));
  142. $guids = array();
  143. foreach ($entity_rows as $row) {
  144. $guids[] = $row->guid;
  145. }
  146. delete_data("DELETE FROM {$db_prefix}entities WHERE guid IN (" . implode(',', $guids) . ")");
  147. delete_data("DELETE FROM {$db_prefix}objects_entity WHERE guid IN (" . implode(',', $guids) . ")");
  148. }
  149. public static function elgg_batch_callback_test($options, $reset = false) {
  150. static $count = 1;
  151. if ($reset) {
  152. $count = 1;
  153. return true;
  154. }
  155. if ($count > 20) {
  156. return false;
  157. }
  158. for ($j = 0; ($options['limit'] < 5) ? $j < $options['limit'] : $j < 5; $j++) {
  159. $return[] = array(
  160. 'offset' => $options['offset'],
  161. 'limit' => $options['limit'],
  162. 'count' => $count++,
  163. 'index' => 1 + $options['offset'] + $j
  164. );
  165. }
  166. return $return;
  167. }
  168. }