|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe PostAnalyzer do |
| 4 | + |
| 5 | + let(:topic) { Fabricate(:topic) } |
| 6 | + let(:default_topic_id) { topic.id } |
| 7 | + let(:post_args) do |
| 8 | + {user: topic.user, topic: topic} |
| 9 | + end |
| 10 | + |
| 11 | + context "links" do |
| 12 | + let(:raw_no_links) { "hello world my name is evil trout" } |
| 13 | + let(:raw_one_link_md) { "[jlawr](http://www.imdb.com/name/nm2225369)" } |
| 14 | + let(:raw_two_links_html) { "<a href='http://disneyland.disney.go.com/'>disney</a> <a href='http://reddit.com'>reddit</a>"} |
| 15 | + let(:raw_three_links) { "http://discourse.org and http://discourse.org/another_url and http://www.imdb.com/name/nm2225369"} |
| 16 | + |
| 17 | + describe "raw_links" do |
| 18 | + it "returns a blank collection for a post with no links" do |
| 19 | + post_analyzer = PostAnalyzer.new(raw_no_links, default_topic_id) |
| 20 | + post_analyzer.raw_links.should be_blank |
| 21 | + end |
| 22 | + |
| 23 | + it "finds a link within markdown" do |
| 24 | + post_analyzer = PostAnalyzer.new(raw_one_link_md, default_topic_id) |
| 25 | + post_analyzer.raw_links.should == ["http://www.imdb.com/name/nm2225369"] |
| 26 | + end |
| 27 | + |
| 28 | + it "can find two links from html" do |
| 29 | + post_analyzer = PostAnalyzer.new(raw_two_links_html, default_topic_id) |
| 30 | + post_analyzer.raw_links.should == ["http://disneyland.disney.go.com/", "http://reddit.com"] |
| 31 | + end |
| 32 | + |
| 33 | + it "can find three links without markup" do |
| 34 | + post_analyzer = PostAnalyzer.new(raw_three_links, default_topic_id) |
| 35 | + post_analyzer.raw_links.should == ["http://discourse.org", "http://discourse.org/another_url", "http://www.imdb.com/name/nm2225369"] |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + describe "linked_hosts" do |
| 40 | + it "returns blank with no links" do |
| 41 | + post_analyzer = PostAnalyzer.new(raw_no_links, default_topic_id) |
| 42 | + post_analyzer.linked_hosts.should be_blank |
| 43 | + end |
| 44 | + |
| 45 | + it "returns the host and a count for links" do |
| 46 | + post_analyzer = PostAnalyzer.new(raw_two_links_html, default_topic_id) |
| 47 | + post_analyzer.linked_hosts.should == {"disneyland.disney.go.com" => 1, "reddit.com" => 1} |
| 48 | + end |
| 49 | + |
| 50 | + it "it counts properly with more than one link on the same host" do |
| 51 | + post_analyzer = PostAnalyzer.new(raw_three_links, default_topic_id) |
| 52 | + post_analyzer.linked_hosts.should == {"discourse.org" => 1, "www.imdb.com" => 1} |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe "image_count" do |
| 58 | + let(:raw_post_one_image_md) { "" } |
| 59 | + let(:raw_post_two_images_html) { "<img src='http://discourse.org/logo.png'> <img src='http://bbc.co.uk/sherlock.jpg'>" } |
| 60 | + let(:raw_post_with_avatars) { '<img alt="smiley" title=":smiley:" src="/assets/emoji/smiley.png" class="avatar"> <img alt="wink" title=":wink:" src="/assets/emoji/wink.png" class="avatar">' } |
| 61 | + let(:raw_post_with_favicon) { '<img src="/assets/favicons/wikipedia.png" class="favicon">' } |
| 62 | + let(:raw_post_with_thumbnail) { '<img src="/assets/emoji/smiley.png" class="thumbnail">' } |
| 63 | + let(:raw_post_with_two_classy_images) { "<img src='http://discourse.org/logo.png' class='classy'> <img src='http://bbc.co.uk/sherlock.jpg' class='classy'>" } |
| 64 | + |
| 65 | + it "returns 0 images for an empty post" do |
| 66 | + post_analyzer = PostAnalyzer.new("Hello world", nil) |
| 67 | + post_analyzer.image_count.should == 0 |
| 68 | + end |
| 69 | + |
| 70 | + it "finds images from markdown" do |
| 71 | + post_analyzer = PostAnalyzer.new(raw_post_one_image_md, default_topic_id) |
| 72 | + post_analyzer.image_count.should == 1 |
| 73 | + end |
| 74 | + |
| 75 | + it "finds images from HTML" do |
| 76 | + post_analyzer = PostAnalyzer.new(raw_post_two_images_html, default_topic_id) |
| 77 | + post_analyzer.image_count.should == 2 |
| 78 | + end |
| 79 | + |
| 80 | + it "doesn't count avatars as images" do |
| 81 | + post_analyzer = PostAnalyzer.new(raw_post_with_avatars, default_topic_id) |
| 82 | + post_analyzer.image_count.should == 0 |
| 83 | + end |
| 84 | + |
| 85 | + it "doesn't count favicons as images" do |
| 86 | + post_analyzer = PostAnalyzer.new(raw_post_with_favicon, default_topic_id) |
| 87 | + post_analyzer.image_count.should == 0 |
| 88 | + end |
| 89 | + |
| 90 | + it "doesn't count thumbnails as images" do |
| 91 | + post_analyzer = PostAnalyzer.new(raw_post_with_thumbnail, default_topic_id) |
| 92 | + post_analyzer.image_count.should == 0 |
| 93 | + end |
| 94 | + |
| 95 | + it "doesn't count whitelisted images" do |
| 96 | + Post.stubs(:white_listed_image_classes).returns(["classy"]) |
| 97 | + post_analyzer = PostAnalyzer.new(raw_post_with_two_classy_images, default_topic_id) |
| 98 | + post_analyzer.image_count.should == 0 |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe "link_count" do |
| 103 | + let(:raw_post_one_link_md) { "[sherlock](http://www.bbc.co.uk/programmes/b018ttws)" } |
| 104 | + let(:raw_post_two_links_html) { "<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>" } |
| 105 | + let(:raw_post_with_mentions) { "hello @novemberkilo how are you doing?" } |
| 106 | + |
| 107 | + it "returns 0 links for an empty post" do |
| 108 | + post_analyzer = PostAnalyzer.new("Hello world", nil) |
| 109 | + post_analyzer.link_count.should == 0 |
| 110 | + end |
| 111 | + |
| 112 | + it "returns 0 links for a post with mentions" do |
| 113 | + post_analyzer = PostAnalyzer.new(raw_post_with_mentions, default_topic_id) |
| 114 | + post_analyzer.link_count.should == 0 |
| 115 | + end |
| 116 | + |
| 117 | + it "finds links from markdown" do |
| 118 | + post_analyzer = PostAnalyzer.new(raw_post_one_link_md, default_topic_id) |
| 119 | + post_analyzer.link_count.should == 1 |
| 120 | + end |
| 121 | + |
| 122 | + it "finds links from HTML" do |
| 123 | + post_analyzer = PostAnalyzer.new(raw_post_two_links_html, default_topic_id) |
| 124 | + post_analyzer.link_count.should == 2 |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + |
| 129 | + describe "raw_mentions" do |
| 130 | + |
| 131 | + it "returns an empty array with no matches" do |
| 132 | + post_analyzer = PostAnalyzer.new("Hello Jake and Finn!", default_topic_id) |
| 133 | + post_analyzer.raw_mentions.should == [] |
| 134 | + end |
| 135 | + |
| 136 | + it "returns lowercase unique versions of the mentions" do |
| 137 | + post_analyzer = PostAnalyzer.new("@Jake @Finn @Jake", default_topic_id) |
| 138 | + post_analyzer.raw_mentions.should == ['jake', 'finn'] |
| 139 | + end |
| 140 | + |
| 141 | + it "ignores pre" do |
| 142 | + post_analyzer = PostAnalyzer.new("<pre>@Jake</pre> @Finn", default_topic_id) |
| 143 | + post_analyzer.raw_mentions.should == ['finn'] |
| 144 | + end |
| 145 | + |
| 146 | + it "catches content between pre tags" do |
| 147 | + post_analyzer = PostAnalyzer.new("<pre>hello</pre> @Finn <pre></pre>", default_topic_id) |
| 148 | + post_analyzer.raw_mentions.should == ['finn'] |
| 149 | + end |
| 150 | + |
| 151 | + it "ignores code" do |
| 152 | + post_analyzer = PostAnalyzer.new("@Jake <code>@Finn</code>", default_topic_id) |
| 153 | + post_analyzer.raw_mentions.should == ['jake'] |
| 154 | + end |
| 155 | + |
| 156 | + it "ignores quotes" do |
| 157 | + post_analyzer = PostAnalyzer.new("[quote=\"Evil Trout\"]@Jake[/quote] @Finn", default_topic_id) |
| 158 | + post_analyzer.raw_mentions.should == ['finn'] |
| 159 | + end |
| 160 | + |
| 161 | + it "handles underscore in username" do |
| 162 | + post_analyzer = PostAnalyzer.new("@Jake @Finn @Jake_Old", default_topic_id) |
| 163 | + post_analyzer.raw_mentions.should == ['jake', 'finn', 'jake_old'] |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments