BxPostsFormsTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
  3. /**
  4. * Content-module form helper and entry-form methods via Posts.
  5. */
  6. #[AllowMockObjectsWithoutExpectations]
  7. class BxPostsFormsTest extends BxPostsTestCase
  8. {
  9. public function testPrepareResponsePlainAndJson()
  10. {
  11. $o = new BxPostsFormsEntryHelper($this->_oModule);
  12. $this->assertSame('ok', $this->bxCallProtected($o, 'prepareResponse', 'ok', false));
  13. $this->assertSame(
  14. ['msg' => 'ok', '_dt' => 'json'],
  15. $this->bxCallProtected($o, 'prepareResponse', 'ok', true)
  16. );
  17. $this->assertSame(
  18. ['code' => 1, '_dt' => 'json', 'reload' => 1],
  19. $this->bxCallProtected($o, 'prepareResponse', 1, true, 'code', ['reload' => 1])
  20. );
  21. }
  22. public function testRedirectFromContextWhenPublicPrivacy()
  23. {
  24. $o = new BxPostsFormsEntryHelper($this->_oModule);
  25. $aPost = $this->bxSamplePost(['allow_view_to' => 3]);
  26. $this->assertFalse($this->bxCallProtected($o, '_getRedirectFromContext', 'add', $aPost));
  27. }
  28. public function testPrepareCustomRedirectUrlReplacesMarkers()
  29. {
  30. $o = new BxPostsFormsEntryHelper($this->_oModule);
  31. $s = $this->bxCallProtected($o, 'prepareCustomRedirectUrl', 'page.php?i=view-post&id={content_id}&m={module}', $this->bxSamplePost());
  32. $this->assertStringNotContainsString('{content_id}', $s);
  33. $this->assertStringNotContainsString('{module}', $s);
  34. $this->assertStringContainsString('bx_posts', $s);
  35. }
  36. public function testViewDataTextMissingContent()
  37. {
  38. $oDb = $this->createMock(BxPostsDb::class);
  39. $oDb->method('getContentInfoById')->willReturn(false);
  40. $this->bxReplaceDb($oDb);
  41. $o = new BxPostsFormsEntryHelper($this->_oModule);
  42. [$oProfile, $aContent] = $this->bxCallProtected($o, '_getProfileAndContentData', 15);
  43. $this->assertFalse($oProfile);
  44. $this->assertFalse($aContent);
  45. }
  46. public function testProcessLinksSkipsMissingField()
  47. {
  48. $oForm = $this->bxFormDouble();
  49. $oForm->aInputs = [];
  50. $oDb = $this->createMock(BxPostsDb::class);
  51. $oDb->expects($this->never())->method('saveLink');
  52. $this->bxReplaceDb($oDb);
  53. $this->assertTrue($oForm->processLinks('link', 15));
  54. }
  55. public function testProcessLinksSavesEachId()
  56. {
  57. $oForm = $this->bxFormDouble(['1', '2']);
  58. $oForm->aInputs = ['link' => ['name' => 'link']];
  59. $oDb = $this->createMock(BxPostsDb::class);
  60. $oDb->expects($this->exactly(2))->method('saveLink')->willReturnCallback(function ($iContentId, $iLinkId) {
  61. $this->assertSame(15, $iContentId);
  62. $this->assertContains((string)$iLinkId, ['1', '2']);
  63. return true;
  64. });
  65. $this->bxReplaceDb($oDb);
  66. $oForm->processLinks('link', 15);
  67. }
  68. public function testCoverGhostTemplateVars()
  69. {
  70. $oForm = $this->bxFormDouble();
  71. $oForm->aInputs = [
  72. 'covers' => [
  73. 'name' => 'covers',
  74. 'content_id' => 15,
  75. ],
  76. ];
  77. $a = $this->bxCallProtected($oForm, '_getCoverGhostTmplVars', $this->bxSamplePost(['thumb' => 9]));
  78. $this->assertSame('covers', $a['name']);
  79. $this->assertSame(15, $a['content_id']);
  80. $this->assertSame('post-text', $a['editor_id']);
  81. $this->assertSame(9, $a['thumb_id']);
  82. $this->assertSame('thumb', $a['name_thumb']);
  83. }
  84. protected function bxFormDouble($mixedClean = null): BxPostsFormEntry
  85. {
  86. $oForm = new class($this->_oModule) extends BxPostsFormEntry {
  87. public $mixedCleanValue;
  88. public function __construct($oModule)
  89. {
  90. $this->MODULE = 'bx_posts';
  91. $this->_oModule = $oModule;
  92. $this->aInputs = [];
  93. }
  94. public function getCleanValue($sName)
  95. {
  96. return $this->mixedCleanValue;
  97. }
  98. };
  99. $oForm->mixedCleanValue = $mixedClean;
  100. return $oForm;
  101. }
  102. }