ElggCoreAttributeLoaderTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Test attribute loader for entities
  4. */
  5. class ElggCoreAttributeLoaderTest extends \ElggCoreUnitTest {
  6. /**
  7. * Checks if additional select columns are readable as volatile data
  8. *
  9. * https://github.com/Elgg/Elgg/issues/5543
  10. */
  11. public function testSqlAdditionalSelectsAsVolatileData() {
  12. // remove ignore access as it disables entity cache
  13. $access = elgg_set_ignore_access(false);
  14. // may not have groups in DB but guaranteed to have user, object, site
  15. $group = new \ElggGroup();
  16. $group->name = 'test_group';
  17. $group->access_id = ACCESS_PUBLIC;
  18. $this->assertTrue($group->save() !== false);
  19. foreach (array('site', 'user', 'group', 'object') as $type) {
  20. $entities = elgg_get_entities(array(
  21. 'type' => $type,
  22. 'selects' => array('42 as added_col2'),
  23. 'limit' => 1,
  24. ));
  25. $entity = array_shift($entities);
  26. $this->assertTrue($entity instanceof \ElggEntity);
  27. $this->assertEqual($entity->added_col2, null, "Additional select columns are leaking to attributes for " . get_class($entity));
  28. $this->assertEqual($entity->getVolatileData('select:added_col2'), 42);
  29. }
  30. elgg_set_ignore_access($access);
  31. $group->delete();
  32. }
  33. /**
  34. * Checks if additional select columns are readable as volatile data even if we hit the cache while fetching entity.
  35. *
  36. * https://github.com/Elgg/Elgg/issues/5544
  37. */
  38. public function testSqlAdditionalSelectsAsVolatileDataWithCache() {
  39. // remove ignore access as it disables entity cache
  40. $access = elgg_set_ignore_access(false);
  41. // may not have groups in DB - let's create one
  42. $group = new \ElggGroup();
  43. $group->name = 'test_group';
  44. $group->access_id = ACCESS_PUBLIC;
  45. $this->assertTrue($group->save() !== false);
  46. foreach (array('site', 'user', 'group', 'object') as $type) {
  47. $entities = elgg_get_entities(array(
  48. 'type' => $type,
  49. 'selects' => array('42 as added_col3'),
  50. 'limit' => 1,
  51. ));
  52. $entity = array_shift($entities);
  53. $this->assertTrue($entity instanceof \ElggEntity);
  54. $this->assertEqual($entity->added_col3, null, "Additional select columns are leaking to attributes for " . get_class($entity));
  55. $this->assertEqual($entity->getVolatileData('select:added_col3'), 42);
  56. // make sure we have cached the entity
  57. $this->assertNotEqual(false, _elgg_retrieve_cached_entity($entity->guid));
  58. }
  59. // run these again but with different value to make sure cache does not interfere
  60. foreach (array('site', 'user', 'group', 'object') as $type) {
  61. $entities = elgg_get_entities(array(
  62. 'type' => $type,
  63. 'selects' => array('64 as added_col3'),
  64. 'limit' => 1,
  65. ));
  66. $entity = array_shift($entities);
  67. $this->assertTrue($entity instanceof \ElggEntity);
  68. $this->assertEqual($entity->added_col3, null, "Additional select columns are leaking to attributes for " . get_class($entity));
  69. $this->assertEqual($entity->getVolatileData('select:added_col3'), 64, "Failed to overwrite volatile data in cached entity");
  70. }
  71. elgg_set_ignore_access($access);
  72. $group->delete();
  73. }
  74. }