Commit 624abf3454d67c41734c8797670bfe672a914c35

Authored by Rodrigo Souto
1 parent cb383dad

stoa_plugin: add tags to person api

Also include person_api test file that was not added on previous
commits...

(ActionItem2832)
plugins/stoa/lib/stoa_plugin/person_api.rb
@@ -22,4 +22,11 @@ class StoaPlugin::PersonApi < Noosfero::FieldsDecorator @@ -22,4 +22,11 @@ class StoaPlugin::PersonApi < Noosfero::FieldsDecorator
22 def image_base64 22 def image_base64
23 Base64.encode64(image.current_data) if image && image.current_data 23 Base64.encode64(image.current_data) if image && image.current_data
24 end 24 end
  25 +
  26 + def tags
  27 + articles.tag_counts({:order => 'tags.count desc', :limit => 10}).inject({}) do |memo,tag|
  28 + memo[tag.name] = tag.count
  29 + memo
  30 + end
  31 + end
25 end 32 end
plugins/stoa/lib/stoa_plugin/person_fields.rb
1 module StoaPlugin::PersonFields 1 module StoaPlugin::PersonFields
2 HEAVY = %w[image_base64] 2 HEAVY = %w[image_base64]
3 FILTER = %w[image] 3 FILTER = %w[image]
  4 + EXTRA = %w[tags]
4 5
5 ESSENTIAL = %w[username email nusp] 6 ESSENTIAL = %w[username email nusp]
6 AVERAGE = ESSENTIAL + %w[name first_name surname address homepage] 7 AVERAGE = ESSENTIAL + %w[name first_name surname address homepage]
7 - FULL = (AVERAGE + Person.fields + HEAVY - FILTER).uniq 8 + FULL = (AVERAGE + Person.fields + HEAVY + EXTRA - FILTER).uniq
8 COMPLETE = FULL - HEAVY 9 COMPLETE = FULL - HEAVY
9 10
10 FIELDS = { 11 FIELDS = {
plugins/stoa/test/unit/person_api_test.rb 0 → 100644
@@ -0,0 +1,103 @@ @@ -0,0 +1,103 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class StoaPlugin::PersonApiTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @person = create_user('sample-user').person
  7 + end
  8 +
  9 + attr_accessor :person
  10 +
  11 + should 'provide nusp' do
  12 + person.usp_id = '99999999'
  13 + api = StoaPlugin::PersonApi.new(person)
  14 + assert_equal person.usp_id, api.nusp
  15 + end
  16 +
  17 + should 'provide username' do
  18 + api = StoaPlugin::PersonApi.new(person)
  19 + assert_equal person.user.login, api.username
  20 + end
  21 +
  22 + should 'provide first_name' do
  23 + person.name = "Jean-Luc Picard"
  24 + api = StoaPlugin::PersonApi.new(person)
  25 + assert_equal 'Jean-Luc', api.first_name
  26 + end
  27 +
  28 + should 'provide surname' do
  29 + person.name = "Jean-Luc Picard"
  30 + api = StoaPlugin::PersonApi.new(person)
  31 + assert_equal 'Picard', api.surname
  32 + end
  33 +
  34 + should 'provide homepage' do
  35 + api = StoaPlugin::PersonApi.new(person, self)
  36 + homepage = 'picard.me'
  37 + self.stubs(:url_for).with(person.url).returns(homepage)
  38 + assert_equal homepage, api.homepage
  39 + end
  40 +
  41 + should 'provide image on base64' do
  42 + person.image_builder = {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
  43 + person.save!
  44 + api = StoaPlugin::PersonApi.new(person)
  45 + assert_equal Base64.encode64(person.image.current_data), api.image_base64
  46 + end
  47 +
  48 + should 'not crash on image_base64 if profile has no image' do
  49 + api = StoaPlugin::PersonApi.new(person)
  50 + assert_equal nil, api.image_base64
  51 + end
  52 +
  53 + should 'provide tags' do
  54 + create_article_with_tags(person.id, 'free_software, noosfero, linux')
  55 + create_article_with_tags(person.id, 'free_software, linux')
  56 + create_article_with_tags(person.id, 'free_software')
  57 +
  58 + api = StoaPlugin::PersonApi.new(person)
  59 + assert_equal person.article_tags, api.tags
  60 + end
  61 +
  62 + should 'provide tags limited by 10 most relevant' do
  63 + 13.times {create_article_with_tags(person.id, 'a')}
  64 + 12.times {create_article_with_tags(person.id, 'b')}
  65 + 11.times {create_article_with_tags(person.id, 'c')}
  66 + 10.times {create_article_with_tags(person.id, 'd')}
  67 + 9.times {create_article_with_tags(person.id, 'e')}
  68 + 8.times {create_article_with_tags(person.id, 'f')}
  69 + 7.times {create_article_with_tags(person.id, 'g')}
  70 + 6.times {create_article_with_tags(person.id, 'h')}
  71 + 5.times {create_article_with_tags(person.id, 'i')}
  72 + 4.times {create_article_with_tags(person.id, 'j')}
  73 + 3.times {create_article_with_tags(person.id, 'l')}
  74 + 2.times {create_article_with_tags(person.id, 'm')}
  75 + 1.times {create_article_with_tags(person.id, 'n')}
  76 +
  77 + api = StoaPlugin::PersonApi.new(person)
  78 + tags = api.tags
  79 + assert_equal 10, tags.size
  80 + assert tags['a']
  81 + assert tags['b']
  82 + assert tags['c']
  83 + assert tags['d']
  84 + assert tags['e']
  85 + assert tags['f']
  86 + assert tags['g']
  87 + assert tags['h']
  88 + assert tags['i']
  89 + assert tags['j']
  90 + assert_nil tags['l']
  91 + assert_nil tags['m']
  92 + assert_nil tags['n']
  93 + end
  94 +
  95 + private
  96 +
  97 + def create_article_with_tags(profile_id, tags = '')
  98 + article = fast_create(Article, :profile_id => profile_id)
  99 + article.tag_list = tags
  100 + article.save!
  101 + article
  102 + end
  103 +end