BxBaseServiceProfilesRecommendationsTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * "Recommended friends" on the members' home page: with nothing to suggest (a new or small site) the block shows an
  4. * empty state with a link to find people, instead of rendering nothing and leaving its page column blank.
  5. */
  6. class BxBaseServiceProfilesRecommendationsTest extends BxDolTestCase
  7. {
  8. // No profile has this id, so there is nothing to recommend to it.
  9. private const NO_RECOMMENDATIONS_PROFILE = 2147483000;
  10. private function browse(array $aParams)
  11. {
  12. return BxDolService::call('system', 'browse_recommendations_friends', [self::NO_RECOMMENDATIONS_PROFILE, $aParams], 'TemplServiceProfiles');
  13. }
  14. private function callProtected($oObject, string $sMethod, array $aArgs = [])
  15. {
  16. return (new ReflectionMethod($oObject, $sMethod))->invokeArgs($oObject, $aArgs);
  17. }
  18. private function withDefaultProfileType(string $sValue, callable $fTest)
  19. {
  20. $sPrior = getParam('sys_account_default_profile_type');
  21. setParam('sys_account_default_profile_type', $sValue);
  22. try {
  23. return $fTest();
  24. }
  25. finally {
  26. setParam('sys_account_default_profile_type', $sPrior);
  27. }
  28. }
  29. /**
  30. * Active profile modules that have a home page, each with the URL the link should point to.
  31. */
  32. private function profileHomes(): array
  33. {
  34. $aResult = [];
  35. foreach (array_keys(bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]) ?: []) as $sModule) {
  36. $oModule = BxDolModule::getInstance($sModule);
  37. if ($oModule && !empty($oModule->_oConfig->CNF['URL_HOME']))
  38. $aResult[$sModule] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($oModule->_oConfig->CNF['URL_HOME']);
  39. }
  40. return $aResult;
  41. }
  42. public function testNothingToRecommendRendersNothingByDefault(): void
  43. {
  44. $this->assertEmpty($this->browse(['per_page' => 3]), 'Blocks without empty_state keep hiding themselves.');
  45. }
  46. public function testEmptyStateExplainsWhy(): void
  47. {
  48. $sHtml = $this->browse(['per_page' => 3, 'empty_state' => true]);
  49. $this->assertStringContainsString('bx-recommendations-empty', $sHtml);
  50. $this->assertStringContainsString(_t('_sys_recom_friends_empty_title'), $sHtml);
  51. $this->assertStringContainsString(_t('_sys_recom_friends_empty_text'), $sHtml);
  52. }
  53. public function testEmptyStateWinsOverTheGenericEmptyMessage(): void
  54. {
  55. $sHtml = $this->browse(['empty_state' => true, 'empty_message' => true]);
  56. $this->assertStringContainsString('bx-recommendations-empty', $sHtml);
  57. $this->assertStringNotContainsString('bx-msg-box', $sHtml);
  58. }
  59. public function testFindPeopleGoesToTheDefaultProfileType(): void
  60. {
  61. $aHomes = $this->profileHomes();
  62. if (!$aHomes)
  63. $this->markTestSkipped('No active profile module with a home page is installed.');
  64. // The last module, so the default has to beat the module order to be picked.
  65. $sDefault = array_key_last($aHomes);
  66. $sHtml = $this->withDefaultProfileType($sDefault, fn() => $this->browse(['empty_state' => true]));
  67. $this->assertStringContainsString('href="' . $aHomes[$sDefault] . '"', $sHtml);
  68. $this->assertStringContainsString(_t('_sys_recom_friends_empty_action'), $sHtml);
  69. }
  70. public function testWithoutAUsableDefaultTheFirstProfileModuleIsUsed(): void
  71. {
  72. $aHomes = $this->profileHomes();
  73. if (!$aHomes)
  74. $this->markTestSkipped('No active profile module with a home page is installed.');
  75. $sFirst = $aHomes[array_key_first($aHomes)];
  76. foreach (['', 'bx_no_such_module'] as $sDefault) {
  77. $sUrl = $this->withDefaultProfileType($sDefault, fn() => $this->callProtected(new BxTemplServiceProfiles(), '_getRecommendationsEmptyStateUrl'));
  78. $this->assertSame($sFirst, $sUrl, "Default profile type '$sDefault'");
  79. }
  80. }
  81. public function testNoProfileModulesMeansNoButton(): void
  82. {
  83. $oService = new class extends BxTemplServiceProfiles {
  84. protected function _getRecommendationsEmptyStateModules()
  85. {
  86. return [];
  87. }
  88. };
  89. $this->assertSame('', $this->callProtected($oService, '_getRecommendationsEmptyStateUrl'));
  90. $sHtml = $this->callProtected($oService, '_getRecommendationsEmptyState');
  91. $this->assertStringContainsString(_t('_sys_recom_friends_empty_title'), $sHtml);
  92. $this->assertStringNotContainsString('bx-btn', $sHtml, 'With nowhere to send people, there is no button.');
  93. }
  94. }