Skip to content

Commit aa7e431

Browse files
committed
Test anonymously view document page
Summary: Test for ability to see.. * Content * Title * Stats * Comments * Notes Also.. * Hidden conversation when discussion hidden * Can't view unpublished documents * Redirect to login if not logged and trying to interact * Hidden comment/reply boxes if not logged in Resolves T198 Resolves T199 Resolves T200 Test Plan: * `make test` Reviewers: doshitan Reviewed By: doshitan Maniphest Tasks: T198, T199, T200 Differential Revision: https://phabricator.opengovfoundation.org/D113
1 parent 05ec24e commit aa7e431

File tree

8 files changed

+524
-12
lines changed

8 files changed

+524
-12
lines changed

app/Models/Role.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ class Role extends EntrustRole
1010
const ROLE_INDEPENDENT_SPONSOR = "Independent Sponsor";
1111

1212
protected $fillable = ['name'];
13+
14+
public static function adminRole()
15+
{
16+
return static::where('name', static::ROLE_ADMIN)->first() ?: static::create(['name' => static::ROLE_ADMIN]);
17+
}
1318
}

database/factories/ModelFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
];
3535
});
3636

37-
$factory->defineAs(Role::class, 'admin_role', function (Faker\Generator $faker) {
38-
return ['name' => Role::ROLE_ADMIN];
39-
});
40-
4137
$factory->define(Sponsor::class, function (Faker\Generator $faker) {
4238
$name = $faker->company;
4339
$display_name = "{$name} {$faker->companySuffix}";
@@ -61,9 +57,7 @@
6157
});
6258

6359
$factory->define(Document::class, function (Faker\Generator $faker) {
64-
return [
65-
'title' => substr($faker->sentence(5), 0, -1),
66-
];
60+
return [ 'title' => $faker->words(5, true) ];
6761
});
6862

6963
$factory->define(DocContent::class, function (Faker\Generator $faker) {

resources/assets/js/annotator-madison.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ $.extend(Annotator.Plugin.Madison.prototype, new Annotator.Plugin(), {
447447

448448
pane += '<section class="annotation-list">'
449449
annotationGroup.annotations.forEach(function (annotation) {
450-
pane += '<article class="annotation">';
450+
pane += '<article class="annotation" id="' + annotation.htmlId + '">';
451451

452452
pane += '<blockquote>&quot;';
453453
annotation.highlights.forEach(function (highlight) {

resources/views/documents/partials/comment.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
@if (Auth::user() && $comment->annotatable_type === \App\Models\Doc::ANNOTATABLE_TYPE)
4646
<div class="col-md-12">
4747
<hr>
48-
{{ Form::open(['route' => ['documents.comments.storeReply', $comment->annotatable_id, $comment->id]]) }}
48+
{{ Form::open(['route' => ['documents.comments.storeReply', $comment->annotatable_id, $comment->id], 'class' => 'comment-form']) }}
4949
{{ Form::mInput(
5050
'textarea',
5151
'text',

resources/views/documents/show.blade.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
@section('content')
1010
<div class="page-header">
1111
<h1>{{ $document->title }}</h1>
12+
<h3 class="sponsors">
13+
<small>
14+
Sponsored by:
15+
{{ $document->sponsors->implode('display_name', ', ') }}
16+
</small>
17+
</h3>
1218
</div>
1319

1420
@include('components.errors')
@@ -32,7 +38,7 @@
3238
@endif
3339
</div>
3440

35-
<div class="btn-group" role="group">
41+
<div class="btn-group support-btn" role="group">
3642
{{ Form::open(['route' => ['documents.support', $document], 'method' => 'put']) }}
3743
<input type="hidden" name="support" value="1">
3844

@@ -47,7 +53,7 @@
4753
@endif
4854
{{ Form::close() }}
4955
</div>
50-
<div class="btn-group" role="group">
56+
<div class="btn-group oppose-btn" role="group">
5157
{{ Form::open(['route' => ['documents.support', $document], 'method' => 'put']) }}
5258
<input type="hidden" name="support" value="0">
5359
@if ($userSupport === false)
@@ -100,7 +106,7 @@
100106
<section class="col-md-8">
101107
@if ($document->discussion_state === \App\Models\Doc::DISCUSSION_STATE_OPEN)
102108
@if (Auth::user())
103-
{{ Form::open(['route' => ['documents.comments.store', $document]]) }}
109+
{{ Form::open(['route' => ['documents.comments.store', $document], 'class' => 'comment-form']) }}
104110
{{ Form::mInput(
105111
'textarea',
106112
'text',

tests/Browser/DocumentPageTest.php

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
3+
namespace Tests\Browser;
4+
5+
use Tests\DuskTestCase;
6+
use Tests\Browser\Pages\DocumentPage;
7+
use Tests\FactoryHelpers;
8+
9+
use App\Models\Doc as Document;
10+
use App\Models\DocContent;
11+
use App\Models\User;
12+
13+
class DocumentPageTest extends DuskTestCase
14+
{
15+
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
20+
$this->user = factory(User::class)->create();
21+
$this->sponsor = FactoryHelpers::createActiveSponsorWithUser($this->user);
22+
23+
$this->document = factory(Document::class)->create([
24+
'publish_state' => Document::PUBLISH_STATE_PUBLISHED,
25+
]);
26+
27+
$this->document->content()->save(factory(DocContent::class)->make());
28+
29+
$this->document->sponsors()->save($this->sponsor);
30+
31+
$firstWord = explode(' ', $this->document->content()->first()->content)[0];
32+
$secondWord = explode(' ', $this->document->content()->first()->content)[1];
33+
34+
$this->note1 = FactoryHelpers::addNoteToDocument($this->user, $this->document, $firstWord);
35+
$this->note2 = FactoryHelpers::addNoteToDocument($this->user, $this->document, $secondWord);
36+
37+
$this->comment1 = FactoryHelpers::createComment($this->user, $this->document);
38+
$this->comment2 = FactoryHelpers::createComment($this->user, $this->document);
39+
40+
$this->commentReply = FactoryHelpers::createComment($this->user, $this->comment1);
41+
}
42+
43+
public function testCanSeeDocumentContent()
44+
{
45+
$this->browse(function ($browser) {
46+
$browser->visit(new DocumentPage($this->document))
47+
->assertTitleContains($this->document->title)
48+
->assertSee($this->document->title)
49+
->assertSeeIn('@sponsorList', $this->document->sponsors()->first()->display_name)
50+
;
51+
52+
foreach (explode("\n\n", $this->document->content()->first()->content) as $p) {
53+
$browser->assertSee($p);
54+
}
55+
});
56+
}
57+
58+
public function testCanSeeDocumentStats()
59+
{
60+
$this->browse(function ($browser) {
61+
$browser->visit(new DocumentPage($this->document))
62+
->assertSeeIn('@participantCount', '1')
63+
->assertSeeIn('@notesCount', '2')
64+
->assertSeeIn('@commentsCount', '3')
65+
->assertSeeIn('@supportBtn', '0')
66+
->assertSeeIn('@opposeBtn', '0')
67+
;
68+
});
69+
}
70+
71+
public function testDiscussionHiddenHidesAllTheThings()
72+
{
73+
$this->document->update(['discussion_state' => Document::DISCUSSION_STATE_HIDDEN]);
74+
75+
$this->browse(function ($browser) {
76+
$browser->visit(new DocumentPage($this->document))
77+
->assertDontSee('@participantCount')
78+
->assertDontSee('@notesCount')
79+
->assertDontSee('@commentsCount')
80+
->assertDontSee('@supportBtn')
81+
->assertDontSee('@opposeBtn')
82+
->assertDontSee('@contentTab')
83+
->assertDontSee('@commentsTab')
84+
->assertDontSee('@annotationGroups')
85+
;
86+
});
87+
}
88+
89+
public function testViewDocumentComments()
90+
{
91+
/**
92+
* Not testing for timestamps here because they end up off by a second or so
93+
*/
94+
$this->browse(function ($browser) {
95+
$browser->visit(new DocumentPage($this->document))
96+
->openCommentsTab()
97+
->assertSeeComment($this->comment1)
98+
->assertSeeComment($this->comment2)
99+
->assertSeeReplyToComment($this->comment1, $this->commentReply)
100+
;
101+
});
102+
}
103+
104+
public function testViewDocumentNotes()
105+
{
106+
/**
107+
* Not testing for timestamps here because they end up off by a second or so
108+
*/
109+
$this->browse(function ($browser) {
110+
$browser->visit(new DocumentPage($this->document))
111+
->openNotesPane()
112+
->assertSeeNote($this->note1)
113+
->assertSeeNote($this->note2)
114+
;
115+
});
116+
}
117+
118+
public function testCantViewUnpublishedDocument()
119+
{
120+
$this->document->update([
121+
'publish_state' => Document::PUBLISH_STATE_UNPUBLISHED,
122+
]);
123+
124+
$this->browse(function ($browser) {
125+
$browser->visit(new DocumentPage($this->document))
126+
->assertSee('This action is unauthorized')
127+
;
128+
});
129+
}
130+
131+
public function testLoginRedirectIfSupportWhenNotLoggedIn()
132+
{
133+
$this->browse(function ($browser) {
134+
$browser->visit(new DocumentPage($this->document))
135+
->click('@supportBtn')
136+
->assertPathIs('/login')
137+
;
138+
});
139+
}
140+
141+
public function testLoginRedirectIfOpposeWhenNotLoggedIn()
142+
{
143+
$this->browse(function ($browser) {
144+
$browser->visit(new DocumentPage($this->document))
145+
->click('@opposeBtn')
146+
->assertPathIs('/login')
147+
;
148+
});
149+
}
150+
151+
public function testLoginRedirectIfLikeCommentWhenNotLoggedIn()
152+
{
153+
$this->browse(function ($browser) {
154+
$browser->visit(new DocumentPage($this->document))
155+
->addActionToComment('like', $this->comment1)
156+
->assertPathIs('/login')
157+
;
158+
});
159+
}
160+
161+
public function testLoginRedirectIfFlagCommentWhenNotLoggedIn()
162+
{
163+
$this->browse(function ($browser) {
164+
$browser->visit(new DocumentPage($this->document))
165+
->addActionToComment('flag', $this->comment1)
166+
->assertPathIs('/login')
167+
;
168+
});
169+
}
170+
171+
public function testLoginRedirectIfLikeNoteWhenNotLoggedIn()
172+
{
173+
$this->browse(function ($browser) {
174+
$browser->visit(new DocumentPage($this->document))
175+
->addActionToNote('like', $this->note1)
176+
->assertPathIs('/login')
177+
;
178+
});
179+
}
180+
181+
public function testLoginRedirectIfFlagNoteWhenNotLoggedIn()
182+
{
183+
$this->browse(function ($browser) {
184+
$browser->visit(new DocumentPage($this->document))
185+
->addActionToNote('flag', $this->note1)
186+
->assertPathIs('/login')
187+
;
188+
});
189+
}
190+
191+
public function testCommentFormsHiddenIfNotLoggedIn()
192+
{
193+
$this->browse(function ($browser) {
194+
$browser->visit(new DocumentPage($this->document))
195+
->openCommentsTab()
196+
->assertDontSee('@commentForm')
197+
->with(DocumentPage::commentSelector($this->comment1), function($commentDiv) {
198+
$commentDiv->assertDontSee('@commentForm');
199+
})
200+
;
201+
});
202+
}
203+
204+
public function testNoteReplyHiddenIfNotLoggedIn()
205+
{
206+
$this->browse(function ($browser) {
207+
$browser->visit(new DocumentPage($this->document))
208+
->openNotesPane()
209+
->with(DocumentPage::noteSelector($this->note1), function($commentDiv) {
210+
$commentDiv
211+
->assertDontSee('@noteReplyForm')
212+
->clickLink('Add your reply')
213+
->assertPathIs('/login')
214+
;
215+
})
216+
;
217+
});
218+
}
219+
}

0 commit comments

Comments
 (0)