repository = $this->createMock(FormsRepository::class); $this->wp = $this->createMock(WPFunctions::class); $this->wp->expects($this->any())->method('inTheLoop')->willReturn(true); $this->wp->expects($this->any())->method('isMainQuery')->willReturn(true); $this->wp->expects($this->any())->method('wpCreateNonce')->willReturn('asdfgh'); $this->wp->expects($this->any())->method('applyFilters')->will($this->returnCallback(function () { return $this->applyFiltersValue; })); WPFunctions::set($this->wp); $this->assetsController = $this->createMock(AssetsController::class); $this->templateRenderer = $this->createMock(TemplateRenderer::class); $this->renderer = $this->createMock(Renderer::class); $this->renderer->expects($this->any())->method('renderStyles')->willReturn(''); $this->renderer->expects($this->any())->method('renderHTML')->willReturn('
'); $this->subscribersRepository = $this->createMock(SubscribersRepository::class); $this->subscriberSubscribeController = $this->createMock(SubscriberSubscribeController::class); $this->woocommerceHelper = $this->createMock(WCHelper::class); $this->hook = new DisplayFormInWPContent( $this->wp, $this->repository, $this->renderer, $this->assetsController, $this->templateRenderer, $this->subscriberSubscribeController, $this->subscribersRepository, $this->woocommerceHelper ); } public function testAppendsRenderedFormAfterPostContent() { $renderedForm = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => ['below_posts' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '1']],], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testItOnlyDisplaysOncePerRequest(): void { $renderedForm = ''; $this->wp->expects($this->any())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => ['below_posts' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '1']],], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->any())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); $result2 = $this->hook->contentDisplay('content'); verify($result2)->equals('content'); } public function testAppendsRenderedFormAfterOnASpecificPost() { $renderedForm = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '', 'selected' => ['1']]], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testAppendsRenderedFormAfterOnASpecificCategory() { $renderedForm = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->wp->expects($this->any())->method('hasCategory')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '', 'selected' => ['2']], 'categories' => ['2'], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testAppendsRenderedFormAfterOnASpecificWoocommerceCategory() { $renderedForm = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->wp->expects($this->any())->method('hasCategory')->willReturn(false); $this->wp->expects($this->any())->method('hasTerm')->with(['2'], 'product_cat')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '', 'selected' => ['2']], 'categories' => ['2'], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testAppendsRenderedFormAfterOnASpecificTag() { $renderedForm = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->wp->expects($this->any())->method('hasCategory')->willReturn(false); $this->wp->expects($this->any())->method('hasTag')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '', 'selected' => ['2']], 'categories' => ['2'], 'tags' => ['3'], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testAppendsRenderedFormAfterOnASpecificWooCommerceTag() { $renderedForm = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->wp->expects($this->any())->method('hasCategory')->willReturn(false); $this->wp->expects($this->any())->method('hasTag')->willReturn(false); $this->wp->expects($this->any())->method('hasTerm')->with(['3'], 'product_tag')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '', 'selected' => ['2']], 'tags' => ['3'], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testAppendsRenderedFormAfterOnASpecificPage() { $renderedForm = ''; $this->wp->expects($this->any())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(false); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '1', 'pages' => ['all' => '', 'selected' => ['1']], 'posts' => ['all' => '']], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testItPassThruNonStringPostContent() { $this->wp->expects($this->never())->method('isSingle'); $this->wp->expects($this->never())->method('isSingular'); $this->repository->expects($this->never())->method('findAll'); verify($this->hook->contentDisplay(null))->null(); verify($this->hook->contentDisplay([1, 2, 3]))->equals([1, 2, 3]); verify($this->hook->contentDisplay(1))->equals(1); verify($this->hook->contentDisplay(1.1))->equals(1.1); } public function testDoesNotAppendFormIfDisabled() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(false); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '', 'pages' => ['all' => ''], 'posts' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->equals('content'); } public function testDoesNotAppendFormIfEnabledAndPlacementIsDisabled() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(false); $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => ['below_posts' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '']]], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->equals('content'); } public function testDoesNotAppendFormIfNotOnSinglePage() { $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->repository->expects($this->never())->method('findBy'); $result = $this->hook->contentDisplay('content'); verify($result)->equals('content'); } public function testDoesNotAppendFormIfNotOnPost() { $this->wp->expects($this->any())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->wp->expects($this->any())->method('isSingular')->willReturn(false); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => ['below_posts' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '1']],], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->equals('content'); } public function testAppendsRenderedFormOnWoocommerceShopListingPage() { $renderedForm = ''; $this->applyFiltersValue = true; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isSingular')->willReturn(false); $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isPostTypeArchive')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->woocommerceHelper->expects($this->any())->method('wcGetPageId')->willReturn(1); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => '', 'selected' => ['1']], 'posts' => ['all' => ''], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testAppendsRenderedFormOnWoocommerceShopListingPageWhenAllPagesIsSelected() { $renderedForm = ''; $this->applyFiltersValue = true; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isSingular')->willReturn(false); $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isPostTypeArchive')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->woocommerceHelper->expects($this->any())->method('wcGetPageId')->willReturn(1); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => ''], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($renderedForm); } public function testItDoesNotAppendsFormOnWoocommerceShopListingPageWhenPageIsNotSelected() { $renderedForm = ''; $this->applyFiltersValue = true; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isSingular')->willReturn(false); $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isPostTypeArchive')->willReturn(true); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]); $this->woocommerceHelper->expects($this->any())->method('wcGetPageId')->willReturn(1); $this->assetsController->expects($this->never())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->never())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => '', 'selected' => ['5']], 'posts' => ['all' => ''], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->equals('content'); } public function testItDoesNotAppendFormOnOtherWoocommercePageWhenNotOnListingPage() { $renderedForm = ''; $wooShopPageId = 25; // random id $this->applyFiltersValue = true; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isSingular')->willReturn(false); $this->wp->expects($this->exactly(2))->method('isPage') ->withConsecutive([''], [$wooShopPageId]) ->willReturnOnConsecutiveCalls(true, false); $this->wp->expects($this->any())->method('isArchive')->willReturn(false); $this->wp->expects($this->any())->method('isPostTypeArchive')->willReturn(false); $this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 40]); // random id $this->woocommerceHelper->expects($this->any())->method('wcGetPageId')->willReturn($wooShopPageId); $this->assetsController->expects($this->never())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->never())->method('render')->willReturn($renderedForm); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => [ 'enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '1'], ], ], ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->never())->method('findBy')->willReturn([$form]); $result = $this->hook->wooProductListDisplay('content'); verify($result)->equals('content'); } public function testAppendsRenderedFormAfterPageContent() { $formHtml = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $this->wp ->expects($this->never()) ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($formHtml); } public function testSetsTransientToImprovePerformance() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(false); $this->wp ->expects($this->once()) ->method('setTransient'); $this->repository->expects($this->once())->method('findBy')->willReturn([]); $this->hook->contentDisplay('content'); } public function testDoesNotQueryDatabaseIfTransientIsSet() { $this->wp->expects($this->any())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(false); $this->wp->expects($this->any())->method('getPostType')->willReturn('post'); $this->wp ->expects($this->once()) ->method('getTransient') ->willReturn('1'); $this->repository->expects($this->never())->method('findBy'); $this->hook->contentDisplay('content'); } public function testAppendsRenderedPopupForm() { $formHtml = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $this->wp ->expects($this->never()) ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'popup' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($formHtml); } public function testDisplayFormOnAllTagArchives(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isTag')->willReturn(true); $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'tagArchives' => ['all' => '1']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); } public function testItRendersOnlyForSpecificTagArchive(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isTag')->willReturn(true); $this->wp->expects($this->any())->method('hasTag')->willReturnCallback(function(array $tags) { if (in_array('10', $tags)) { return true; } return false; }); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'tagArchives' => ['all' => '', 'selected' => ['10']]], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->any())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'categoryArchives' => ['all' => '', 'selected' => ['15']]], ], 'success_message' => 'Hello', ]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals(''); } public function testItDoesNotDisplayOnTagListingIfNotEnabled(): void { $this->wp->expects($this->any())->method('isTag')->willReturn(true); $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->assetsController->expects($this->never())->method('setupFrontEndDependencies'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'tagArchives' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals(''); } public function testDisplayFormOnAllCategoryArchives(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isCategory')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'categoryArchives' => ['all' => '1']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); } public function testItRendersOnlyForSpecificCategoryArchive(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isCategory')->willReturn(true); $this->wp->expects($this->any())->method('hasCategory')->willReturnCallback(function(array $categories) { return in_array('10', $categories); }); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'categoryArchives' => ['all' => '', 'selected' => ['10']]], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->any())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'categoryArchives' => ['all' => '', 'selected' => ['15']]], ], 'success_message' => 'Hello', ]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals(''); } public function testItDoesNotDisplayOnCategoryListingIfNotEnabled(): void { $this->wp->expects($this->any())->method('isArchive')->willReturn(true); $this->wp->expects($this->any())->method('isCategory')->willReturn(true); $this->assetsController->expects($this->never())->method('setupFrontEndDependencies'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', '', 'categoryArchives' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals(''); } public function testItDisplaysOnHomepage(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isFrontPage')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'popup' => ['enabled' => '1', 'homepage' => '1'], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); } public function testItDisplaysOnSpecificPostsPageWhenAllPagesSelected(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isFrontPage')->willReturn(false); $this->wp->expects($this->any())->method('isHome')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'form_placement' => [ 'popup' => ['enabled' => '1', 'pages' => [ 'all' => '1', ]], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); } public function testItDisplaysOnSpecificPostsPageWhenConfiguredToMatchThatPage(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isFrontPage')->willReturn(false); $this->wp->expects($this->any())->method('isHome')->willReturn(true); $this->wp->expects($this->any())->method('getQueriedObjectId')->willReturn(50); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form = new FormEntity('My Form'); $form->setSettings([ 'form_placement' => [ 'popup' => ['enabled' => '1', 'pages' => [ 'all' => '0', 'selected' => ['50'], ]], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->equals($formHtml); } public function testDoesNotAppendPopupFormIfLoggedInAndSubscribed() { $formHtml = ''; $subscriber = new SubscriberEntity(); $this->subscribersRepository->expects($this->once())->method('getCurrentWPUser')->willReturn($subscriber); $this->subscriberSubscribeController->expects($this->once())->method('isSubscribedToAnyFormSegments')->willReturn(true); $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->assetsController->expects($this->never())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->never())->method('render')->willReturn($formHtml); $this->wp ->expects($this->never()) ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'popup' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->equals('content'); } public function testAppendsPopupFormIfLoggedInAndNotSubscribed() { $formHtml = ''; $subscriber = new SubscriberEntity(); $this->subscribersRepository->expects($this->any())->method('getCurrentWPUser')->willReturn($subscriber); $this->subscriberSubscribeController->expects($this->any())->method('isSubscribedToAnyFormSegments')->willReturn(false); $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $this->wp ->expects($this->never()) ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'popup' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '']], ], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->stringEndsWith($formHtml); } public function testAppendsRenderedFixedBarForm() { $formHtml = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $this->wp ->expects($this->never()) ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'popup' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'fixed_bar' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '1']]], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($formHtml); } public function testOnlyOneFormInEachCategory() { $formHtml = ''; $this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->any())->method('isPage')->willReturn(true); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $form1 = new FormEntity('My Form'); $form1->setSettings([ 'segments' => ['3'], 'form_placement' => ['fixed_bar' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '1']]], 'success_message' => 'Hello', ]); $form1->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $form2 = new FormEntity('My Form'); $form2->setSettings([ 'segments' => ['3'], 'form_placement' => ['fixed_bar' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '1']]], 'success_message' => 'Hello', ]); $form2->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form1, $form2]); $result = $this->hook->contentDisplay('content'); verify($result)->notEquals('content'); verify($result)->stringEndsWith($formHtml); } public function testFormRendersOnProductPageWithoutContent(): void { $formHtml = ''; $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $this->wp->expects($this->once())->method('didAction')->with($this->equalTo('wp_footer'))->willReturn(true); $this->wp->expects($this->once())->method('getTheContent')->willReturn(''); $this->assetsController->expects($this->once())->method('setupFrontEndDependencies'); $this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml); $this->wp ->expects($this->never()) ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], 'form_placement' => [ 'below_posts' => ['enabled' => '', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'popup' => ['enabled' => '1', 'pages' => ['all' => ''], 'posts' => ['all' => '']], 'fixed_bar' => ['enabled' => '1', 'pages' => ['all' => '1'], 'posts' => ['all' => '1']]], 'success_message' => 'Hello', ]); $form->setBody([['type' => 'submit', 'params' => ['label' => 'Subscribe!'], 'id' => 'submit', 'name' => 'Submit']]); $this->repository->expects($this->once())->method('findBy')->willReturn([$form]); ob_start(); $this->hook->maybeRenderFormsInFooter(); $renderedFormHtml = ob_get_clean(); verify($renderedFormHtml)->notEquals('content'); verify($renderedFormHtml)->stringEndsWith($formHtml); } public function _after() { parent::_after(); WPFunctions::set(new WPFunctions()); } }