content_viewer_controller_test.rb 73.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
require_relative "../test_helper"
require 'content_viewer_controller'

# Re-raise errors caught by the controller.
class ContentViewerController; def rescue_action(e) raise e end; end

class ContentViewerControllerTest < ActionController::TestCase

  all_fixtures

  def setup
    @controller = ContentViewerController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

    @profile = create_user('testinguser').person
    @environment = @profile.environment
  end
  attr_reader :profile, :environment

  def test_should_display_page
    page = profile.articles.build(:name => 'test')
    page.save!

    uses_host 'colivre.net'
    get :view_page, :profile => profile.identifier, :page => [ 'test' ]
    assert_response :success
    assert_equal page, assigns(:page)
  end

  def test_should_display_homepage
    a = profile.articles.build(:name => 'test')
    a.save!
    profile.home_page = a
    profile.save!

    get :view_page, :profile => profile.identifier, :page => [ 'test']

    assert_response :success
    assert_template 'view_page'
    assert_equal a, assigns(:page)
  end

  def test_should_get_not_found_error_for_unexisting_page
    uses_host 'anhetegua.net'
    get :view_page, :profile => 'aprofile', :page => ['some_unexisting_page']
    assert_response :missing
  end

  def test_should_get_not_found_error_for_unexisting_profile
    Profile.delete_all
    uses_host 'anhetegua'
    get :view_page, :profile => 'some_unexisting_profile', :page => []
    assert_response :missing
  end

  should 'produce a download-link when article is a uploaded file' do
    profile = create_user('someone').person
    html = UploadedFile.create! :uploaded_data => fixture_file_upload('/files/500.html', 'text/html'), :profile => profile
    html.save!

    get :view_page, :profile => 'someone', :page => [ '500.html' ]

    assert_response :success
    assert_match /#{html.public_filename}/, @response.body
  end

  should 'download file when article is image' do
    profile = create_user('someone').person
    image = UploadedFile.create! :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile
    image.save!

    get :view_page, :profile => 'someone', :page => [ 'rails.png' ]

    assert_response :success
    assert_not_nil assigns(:page).data
    assert_match /image\/png/, @response.headers['Content-Type']
  end

  should 'display image on a page when article is image and has a view param' do
    profile = create_user('someone').person
    image = UploadedFile.create! :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile
    image.save!

    get :view_page, :profile => 'someone', :page => [ 'rails.png' ], :view => true

    assert_response :success
    assert_template 'view_page'
    assert_match /text\/html/, @response.headers['Content-Type']
  end

  should 'produce a download-link when article is not text/html' do

    # for example, RSS feeds
    profile = create_user('someone').person
    page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text')
    page.save!

    feed = RssFeed.new(:name => 'testfeed')
    feed.profile = profile
    feed.save!

    get :view_page, :profile => 'someone', :page => [ 'testfeed' ]

    assert_response :success
    assert_match /^text\/xml/, @response.headers['Content-Type']

    assert_equal feed.data, @response.body
  end

  should "display current article's tags" do
    page = profile.articles.create!(:name => 'myarticle', :body => 'test article', :tag_list => 'tag1, tag2')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_tag :tag => 'div', :attributes => { :id => 'article-tags' }, :descendant => {
      :tag => 'a',
      :attributes => { :href => "/profile/#{profile.identifier}/tags/tag1" }
    }
    assert_tag :tag => 'div', :attributes => { :id => 'article-tags' }, :descendant => {
      :tag => 'a',
      :attributes => { :href => "/profile/#{profile.identifier}/tags/tag2" }
    }

    assert_tag :tag => 'div', :attributes => { :id => 'article-tags' }, :descendant => { :content => /This article's tags:/ }
  end

  should "display image label on article image" do
    page = TinyMceArticle.create!(
             :profile => profile,
             :name => 'myarticle',
             :image_builder => {
               :uploaded_data => fixture_file_upload('/files/tux.png', 'image/png'),
               :label => 'test-label'
             }
           )
    get :view_page, page.url
    assert_match /test-label/, @response.body
  end

  should "not display current article's tags" do
    page = profile.articles.create!(:name => 'myarticle', :body => 'test article')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-tags' }
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-tags' }, :descendant => { :content => /This article's tags:/ }
  end

  should 'not display forbidden articles' do
    profile.articles.create!(:name => 'test')
    profile.update_attributes!({:public_content => false}, :without_protection => true)

    Article.any_instance.expects(:display_to?).with(anything).returns(false)
    get :view_page, :profile => profile.identifier, :page => [ 'test' ]
    assert_response 403
  end

  should 'display allowed articles' do
    profile.articles.create!(:name => 'test')
    profile.update_attributes!({:public_content => false}, :without_protection => true)

    Article.any_instance.expects(:display_to?).with(anything).returns(true)
    get :view_page, :profile => profile.identifier, :page => [ 'test' ]
    assert_response 200
  end

  should 'give 404 status on unexisting article' do
    profile.articles.delete_all
    get :view_page, :profile => profile.identifier, :page => [ 'VERY-UNPROBABLE-PAGE' ]
    assert_response 404
  end

  should 'show access denied to unpublished articles' do
    profile.articles.create!(:name => 'test', :published => false)
    get :view_page, :profile => profile.identifier, :page => [ 'test' ]
    assert_response 403
  end

  should 'show unpublished articles to the user himself' do
    profile.articles.create!(:name => 'test', :published => false)

    login_as(profile.identifier)
    get :view_page, :profile => profile.identifier, :page => [ 'test' ]
    assert_response :success
  end

  should 'not show private content to members' do
    community = fast_create(Community)
    admin = fast_create(Person)
    community.add_member(admin)

    folder = fast_create(Folder, :profile_id => community.id, :published => false, :show_to_followers => false)
    community.add_member(profile)
    login_as(profile.identifier)

    get :view_page, :profile => community.identifier, :page => [ folder.path ]

    assert_template 'access_denied'
  end

  should 'show private content to profile moderators' do
    community = Community.create!(:name => 'testcomm')
    community.articles.create!(:name => 'test', :published => false)
    community.add_moderator(profile)

    login_as(profile.identifier)

    get :view_page, :profile => community.identifier, :page => [ 'test' ]
    assert_response :success
  end

  should 'show private content to profile admins' do
    community = Community.create!(:name => 'testcomm')
    community.articles.create!(:name => 'test', :published => false)
    community.add_admin(profile)

    login_as(profile.identifier)

    get :view_page, :profile => community.identifier, :page => [ 'test' ]
    assert_response :success
  end

  should 'load the correct profile when using hosted domain' do
    profile = create_user('mytestuser').person
    profile.domains << Domain.create!(:name => 'micojones.net')
    profile.save!

    ActionController::TestRequest.any_instance.expects(:host).returns('www.micojones.net').at_least_once

    get :view_page, :page => []

    assert_equal profile, assigns(:profile)
  end

  should 'give link to edit the article for owner' do
    login_as('testinguser')
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/edit/#{@profile.home_page.id}" } }
  end
  should 'not give link to edit the article for non-logged-in people' do
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/edit/#{@profile.home_page.id}" } }
  end
  should 'not give link to edit article for other people' do
    login_as(create_user('anotheruser').login)

    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/edit/#{@profile.home_page.id}" } }
  end

  should 'give link to create new article' do
    login_as('testinguser')
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/new" } }
  end
  should 'give no link to create new article for non-logged in people ' do
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/new" } }
  end
  should 'give no link to create new article for other people' do
    login_as(create_user('anotheruser').login)
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/new" } }
  end

  should 'give link to create new article inside folder' do
    login_as('testinguser')
    folder = Folder.create!(:name => 'myfolder', :profile => @profile)
    xhr :get, :view_page, :profile => 'testinguser', :page => [ 'myfolder' ], :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/new?parent_id=#{folder.id}" } }
  end

  should 'not give access to private articles if logged off' do
    profile = Community.create!(:name => 'test profile', :identifier => 'test_profile')
    intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)

    get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]

    assert_template "shared/access_denied"
  end

  should 'not give access to private articles if logged in but not member' do
    login_as('testinguser')
    profile = Community.create!(:name => 'test profile', :identifier => 'test_profile')
    intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)

    get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]

    assert_template "profile/_private_profile"
  end

  should 'not give access to private articles if logged in and only member' do
    person = create_user('test_user').person
    profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
    intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false, :show_to_followers => false)
    profile.affiliate(person, Profile::Roles.member(profile.environment.id))
    login_as('test_user')

    get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]

    assert_template 'access_denied'
  end

  should 'give access to private articles if logged in and moderator' do
    person = create_user('test_user').person
    profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
    intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
    profile.affiliate(person, Profile::Roles.moderator(profile.environment.id))
    login_as('test_user')

    get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]

    assert_template 'view_page'
  end

  should 'give access to private articles if logged in and admin' do
    person = create_user('test_user').person
    profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
    intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
    profile.affiliate(person, Profile::Roles.admin(profile.environment.id))
    login_as('test_user')

    get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]

    assert_template 'view_page'
  end

  should 'list comments if article has them, even if new comments are not allowed' do
    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text', :accept_comments => false)
    page.comments.create!(:author => profile, :title => 'list my comment', :body => 'foo bar baz')
    get :view_page, :profile => profile.identifier, :page => ['myarticle']

    assert_tag :content => /list my comment/
  end

  should 'redirect to new article path under an old path' do
    p = create_user('test_user').person
    a = p.articles.create(:name => 'old-name')
    old_path = a.path
    a.name = 'new-name'
    a.save!

    get :view_page, :profile => p.identifier, :page => old_path

    assert_response :redirect
    assert_redirected_to :host => p.default_hostname, :controller => 'content_viewer', :action => 'view_page', :profile => p.identifier, :page => a.path
  end

  should 'load new article name equal of another article old name' do
    p = create_user('test_user').person
    a1 = p.articles.create!(:name => 'old-name')
    old_path = a1.path
    a1.name = 'new-name'
    a1.save!
    a2 = p.articles.create!(:name => 'old-name')

    get :view_page, :profile => p.identifier, :page => old_path

    assert_equal a2, assigns(:page)
  end

  should 'redirect to article with most recent version with the name if there is no article with the name' do
    p = create_user('test_user').person
    a1 = p.articles.create!(:name => 'old-name')
    old_path = a1.path
    a1.name = 'new-name'
    a1.save!
    a2 = p.articles.create!(:name => 'old-name')
    a2.name = 'other-new-name'
    a2.save!

    get :view_page, :profile => p.identifier, :page => old_path

    assert_response :redirect
    assert_redirected_to :host => p.default_hostname, :controller => 'content_viewer', :action => 'view_page', :profile => p.identifier, :page => a2.path
  end

  should "display current article's versions" do
    page = TextArticle.create!(:name => 'myarticle', :body => 'test article', :display_versions => true, :profile => profile)
    page.body = 'test article edited'; page.save

    get :article_versions, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_select "ul#article-versions a[href=http://#{profile.environment.default_hostname}/#{profile.identifier}/#{page.path}?version=1]"
  end

  should "fetch correct article version" do
    page = TextArticle.create!(:name => 'myarticle', :body => 'original article', :display_versions => true, :profile => profile)
    page.body = 'edited article'; page.save

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], :version => 1

    assert_tag :tag => 'div', :attributes => { :class => /article-body/ }, :content => /original article/
  end

  should "display current article if version does not exist" do
    page = TextArticle.create!(:name => 'myarticle', :body => 'original article', :display_versions => true, :profile => profile)
    page.body = 'edited article'; page.save

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], :version => 'bli'

    assert_tag :tag => 'div', :attributes => { :class => /article-body/ }, :content => /edited article/
  end

  should "display differences between article's versions" do
    page = TextArticle.create!(:name => 'myarticle', :body => 'original article', :display_versions => true, :profile => profile)
    page.body = 'edited article'; page.save

    get :versions_diff, :profile => profile.identifier, :page => [ 'myarticle' ], :v1 => 1, :v2 => 2;

    assert_tag :tag => 'li', :attributes => { :class => /del/ }, :content => /original/
    assert_tag :tag => 'li', :attributes => { :class => /ins/ }, :content => /edited/
    assert_response :success
  end

  should 'not return an article of a different user' do
    p1 = create_user('test_user').person
    a = p1.articles.create!(:name => 'old-name')
    old_path = a.path
    a.name = 'new-name'
    a.save!

    p2 = create_user('another_user').person

    get :view_page, :profile => p2.identifier, :page => old_path

    assert_response :missing
  end

  should 'not show a profile in an environment that is not its home environment' do
    p = create(Profile, :identifier => 'mytestprofile', :name => 'My test profile', :environment => Environment.default)

    current = fast_create(Environment, :name => 'test environment')
    current.domains.create!(:name => 'example.com')
    uses_host 'www.example.com'

    get :view_page, :profile => 'mytestprofile', :page => []
    assert_response :missing
  end

  should 'list unpublished posts to owner with a different class' do
    login_as('testinguser')
    blog = Blog.create!(:name => 'A blog test', :profile => profile)
    blog.posts << TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => false)

    get :view_page, :profile => profile.identifier, :page => [blog.path]
    assert_tag :tag => 'div', :attributes => {:class => /not-published/}
  end

  should 'not list unpublished posts to a not logged person' do
    blog = Blog.create!(:name => 'A blog test', :profile => profile)
    blog.posts << TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => false)

    get :view_page, :profile => profile.identifier, :page => [blog.path]
    assert_no_tag :tag => 'a', :content => "Post"
  end

  should 'display pagination links of blog' do
    blog = Blog.create!(:name => 'A blog test', :profile => profile, :posts_per_page => 5)
    for n in 1..10
      blog.posts << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog)
    end
    assert_equal 10, blog.posts.size

    get :view_page, :profile => profile.identifier, :page => [blog.path]
    assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{blog.path}?npage=2", :rel => 'next' }
  end

  should 'display first page of blog posts' do
    blog = Blog.create!(:name => 'My blog', :profile => profile, :posts_per_page => 5)
    for n in 1..10
      blog.children << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog)
    end
    assert_equal 10, blog.posts.size

    get :view_page, :profile => profile.identifier, :page => [blog.path]
    for n in 1..5
      assert_no_tag :tag => 'h1', :attributes => { :class => 'title' }, :descendant => {:tag => 'a', :attributes => {:href => /\/#{profile.identifier}\/my-blog\/post-#{n}/}, :content => "Post #{n}"}
    end
    for n in 6..10
      assert_tag :tag => 'h1', :attributes => { :class => 'title' }, :descendant => {:tag => 'a', :attributes => {:href => /\/#{profile.identifier}\/my-blog\/post-#{n}/}, :content => "Post #{n}"}
    end
  end

  should 'display others pages of blog posts' do
    blog = Blog.create!(:name => 'My blog', :profile => profile, :posts_per_page => 5)
    for n in 1..10
      blog.children << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog)
    end
    assert_equal 10, blog.posts.size

    get :view_page, :profile => profile.identifier, :page => [blog.path], :npage => 2
    for n in 1..5
      assert_tag :tag => 'h1', :attributes => { :class => 'title' }, :descendant => {:tag => 'a', :attributes => {:href => /\/#{profile.identifier}\/my-blog\/post-#{n}/}, :content => "Post #{n}"}
    end
    for n in 6..10
      assert_no_tag :tag => 'h1', :attributes => { :class => 'title' }, :descendant => {:tag => 'a', :attributes => {:href => /\/#{profile.identifier}\/my-blog\/post-#{n}/}, :content => "Post #{n}"}
    end
  end

  should 'set year and month filter from URL params' do
    blog = Blog.create!(:name => "blog", :profile => profile)
    profile.articles << blog

    past_post = create(TextileArticle, :name => "past post", :profile => profile, :parent => blog, :published_at => blog.created_at - 1.year)
    current_post = TextileArticle.create!(:name => "current post", :profile => profile, :parent => blog)
    blog.children << past_post
    blog.children << current_post

    year, month = profile.blog.created_at.year.to_s, '%02d' % profile.blog.created_at.month

    get :view_page, :profile => profile.identifier, :page => [profile.blog.path], :year => year, :month => month

    assert_no_tag :tag => 'a', :content => past_post.title
    assert_tag :tag => 'a', :content => current_post.title
  end

  should 'give link to create new article inside folder when view child of folder' do
    login_as('testinguser')
    folder = Folder.create!(:name => 'myfolder', :profile => @profile)
    folder.children << TextileArticle.new(:name => 'children-article', :profile => @profile)
    xhr :get, :view_page, :profile => 'testinguser', :page => [ 'myfolder', 'children-article' ], :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/new?parent_id=#{folder.id}" } }
  end

  should "display 'New article' when create children of folder" do
    login_as(profile.identifier)
    a = Folder.new(:name => 'article folder'); profile.articles << a;  a.save!
    Article.stubs(:short_description).returns('bli')
    xhr :get, :view_page, :profile => profile.identifier, :page => [a.path], :toolbar => true
    assert_tag :tag => 'a', :content => 'New article'
  end

  should "display 'New post' when create children of blog" do
    login_as(profile.identifier)
    a = Blog.create!(:name => 'article folder', :profile => profile)
    Article.stubs(:short_description).returns('bli')
    xhr :get, :view_page, :profile => profile.identifier, :page => [a.path], :toolbar => true
    assert_tag :tag => 'a', :content => 'New post'
  end

  should "display same label for new article button of parent" do
    login_as(profile.identifier)
    a = Blog.create!(:name => 'article folder', :profile => profile)
    Article.stubs(:short_description).returns('bli')
    t = TextileArticle.create!(:name => 'first post', :parent => a, :profile => profile)
    xhr :get, :view_page, :profile => profile.identifier, :page => [t.path], :toolbar => true
    assert_tag :tag => 'a', :content => 'New post'
  end

  should 'display button to remove article' do
    login_as(profile.identifier)
    t = TextileArticle.create!(:name => 'article to destroy', :profile => profile)
    xhr :get, :view_page, :profile => profile.identifier, :page => [t.path], :toolbar => true
    assert_tag :tag => 'a', :content => 'Delete', :attributes => {:href => "/myprofile/#{profile.identifier}/cms/destroy/#{t.id}"}
  end

  should 'not display delete button for homepage' do
    login_as(profile.identifier)
    page = profile.home_page
    xhr :get, :view_page, :profile => profile.identifier, :page => page.path, :toolbar => true
    assert_no_tag :tag => 'a', :content => 'Delete', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/destroy/#{page.id}" }
  end

  should 'add meta tag to rss feed on view blog' do
    login_as(profile.identifier)
    profile.articles << Blog.new(:name => 'Blog', :profile => profile)
    get :view_page, :profile => profile.identifier, :page => ['blog']
    assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Blog', :href => "http://#{environment.default_hostname}/testinguser/blog/feed" }
  end

  should 'add meta tag to rss feed on view post blog' do
    login_as(profile.identifier)
    blog = Blog.create!(:name => 'Blog', :profile => profile)
    TextileArticle.create!(:name => 'first post', :parent => blog, :profile => profile)
    get :view_page, :profile => profile.identifier, :page => ['blog', 'first-post']
    assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Blog', :href => "http://#{environment.default_hostname}/testinguser/blog/feed" }
  end

  should 'hit the article when viewed' do
    a = profile.articles.create!(:name => 'test article')
    get :view_page, :profile => profile.identifier, :page => [a.path]
    a.reload
    assert_equal 1, a.hits
  end

  should 'render html for image when view' do
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile)
    get :view_page, :profile => profile.identifier, :page => file.path, :view => true

    assert_response :success
    assert_template 'view_page'
  end

  should "display 'Upload files' when create children of image gallery" do
    login_as(profile.identifier)
    f = Gallery.create!(:name => 'gallery', :profile => profile)
    xhr :get, :view_page, :profile => profile.identifier, :page => f.path, :toolbar => true
    assert_tag :tag => 'a', :content => 'Upload files', :attributes => {:href => /parent_id=#{f.id}/}
  end

  should "display 'New article' when showing folder child of image gallery" do
    login_as(profile.identifier)
    folder1 = Gallery.create!(:name => 'gallery1', :profile => profile)
    folder1.children << folder2 = Folder.new(:name => 'gallery2', :profile => profile)

    xhr :get, :view_page, :profile => profile.identifier, :page => folder2.path, :toolbar => true
    assert_tag :tag => 'a', :content => 'New article', :attributes => {:href =>/parent_id=#{folder2.id}/}
  end

  should "display 'Upload files' to image gallery when showing its children" do
    login_as(profile.identifier)
    folder = Gallery.create!(:name => 'gallery', :profile => profile)
    file = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    xhr :get, :view_page, :profile => profile.identifier, :page => file.path, :view => true, :toolbar => true

    assert_tag :tag => 'a', :content => 'Upload files', :attributes => {:href => /parent_id=#{folder.id}/}
  end

  should 'render slideshow template' do
    f = Folder.create!(:name => 'gallery', :profile => profile)
    get :view_page, :profile => profile.identifier, :page => f.path, :slideshow => true

    assert_template 'slideshow'
  end

  should 'display all images from profile in the slideshow' do
    @controller.stubs(:per_page).returns(1)
    folder = Gallery.create!(:name => 'gallery', :profile => profile)

    image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'))
    image2 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))

    get :view_page, :profile => profile.identifier, :page => folder.path, :slideshow => true

    assert_equal 2, assigns(:images).size
  end

  should 'not display private images in the slideshow for unauthorized people' do
    owner = create_user('owner').person
    unauthorized = create_user('unauthorized').person
    folder = Gallery.create!(:name => 'gallery', :profile => owner)
    image1 = UploadedFile.create!(:profile => owner, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'), :published => false)
    login_as('unauthorized')
    get :view_page, :profile => owner.identifier, :page => folder.path, :slideshow => true
    assert_response :success
    assert_equal 0, assigns(:images).length
  end

  should 'not display private images thumbnails for unauthorized people' do
    owner = create_user('owner').person
    unauthorized = create_user('unauthorized').person
    folder = Gallery.create!(:name => 'gallery', :profile => owner)
    image1 = UploadedFile.create!(:profile => owner, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'), :published => false)
    login_as('unauthorized')
    get :view_page, :profile => owner.identifier, :page => folder.path
    assert_response :success
    assert_select '.image-gallery-item', 0
  end


  should 'display default image in the slideshow if thumbnails were not processed' do
    @controller.stubs(:per_page).returns(1)
    folder = Gallery.create!(:name => 'gallery', :profile => profile)

    image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'))

    get :view_page, :profile => profile.identifier, :page => folder.path, :slideshow => true

    assert_tag :tag => 'img', :attributes => {:src => /\/images\/icons-app\/image-loading-display.png/}
  end

  should 'display thumbnail image in the slideshow if thumbnails were processed' do
    @controller.stubs(:per_page).returns(1)
    folder = Gallery.create!(:name => 'gallery', :profile => profile)

    image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'))

    process_delayed_job_queue
    get :view_page, :profile => profile.identifier, :page => folder.path, :slideshow => true

    assert_tag :tag => 'img', :attributes => {:src => /other-pic_display.jpg/}
  end

  should 'display default image in gallery if thumbnails were not processed' do
    @controller.stubs(:per_page).returns(1)
    folder = Gallery.create!(:name => 'gallery', :profile => profile)

    image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'))

    get :view_page, :profile => profile.identifier, :page => folder.path

    assert_tag :tag => 'a', :attributes => {:class => 'image', :style => /background-image: url\(\/images\/icons-app\/image-loading-thumb.png\)/}
  end

  should 'display thumbnail image in gallery if thumbnails were processed' do
    @controller.stubs(:per_page).returns(1)
    folder = Gallery.create!(:name => 'gallery', :profile => profile)

    image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'))

    process_delayed_job_queue
    get :view_page, :profile => profile.identifier, :page => folder.path

    assert_tag :tag => 'a', :attributes => {:class => 'image', :style => /background-image: url\(.*\/other-pic_thumb.jpg\)/}
  end

  should 'display source from article' do
    profile.articles << TextileArticle.new(:name => "Article one", :profile => profile, :source => 'http://www.original-source.invalid')
    get :view_page, :profile => profile.identifier, :page => ['article-one']
    assert_tag :tag => 'div', :attributes => { :id => 'article-source' }, :content => /http:\/\/www.original-source.invalid/
  end

  should 'not display source if article has no source' do
    profile.articles << TextileArticle.new(:name => "Article one", :profile => profile)
    get :view_page, :profile => profile.identifier, :page => ['article-one']
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-source' }
  end

  should 'redirect to profile controller when there is no homepage' do
    profile.home_page.destroy
    get :view_page, :profile => profile.identifier, :page => []
    assert_redirected_to :controller => 'profile', :action => 'index', :profile => profile.identifier
  end

  should "not display 'Upload files' when viewing blog" do
    login_as(profile.identifier)
    b = Blog.create!(:name => 'article folder', :profile => profile)
    xhr :get, :view_page, :profile => profile.identifier, :page => b.path, :toolbar => true
    assert_no_tag :tag => 'a', :content => 'Upload files', :attributes => {:href => /parent_id=#{b.id}/}
  end

  should "not display 'Upload files' when viewing post from a blog" do
    login_as(profile.identifier)
    b = Blog.create!(:name => 'article folder', :profile => profile)
    blog_post = TextileArticle.create!(:name => 'children-article', :profile => profile, :parent => b)
    xhr :get, :view_page, :profile => profile.identifier, :page => blog_post.path, :toolbar => true
    assert_no_tag :tag => 'a', :content => 'Upload files', :attributes => {:href => /parent_id=#{b.id}/}
  end

  should 'display title of image on image gallery' do
    login_as(profile.identifier)
    folder = fast_create(Gallery, :profile_id => profile.id)
    file = UploadedFile.create!(:title => 'my img title', :profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))

    get :view_page, :profile => profile.identifier, :page => folder.path

    assert_tag :tag => 'li', :attributes => {:title => 'my img title', :class => 'image-gallery-item'}, :child => {:tag => 'span', :content => 'my img title'}
  end

  should 'allow publisher owner view private articles' do
    c = Community.create!(:name => 'test_com')
    u = create_user_with_permission('test_user', 'publish_content', c)
    login_as u.identifier
    a = create(Article, :profile => c, :name => 'test-article', :author => u, :published => false)

    get :view_page, :profile => c.identifier, :page => a.path

    assert_response :success
    assert_template 'view_page'
  end

  should 'display link to new_article if profile is publisher' do
    c = Community.create!(:name => 'test_com')
    u = create_user_with_permission('test_user', 'publish_content', c)
    login_as u.identifier
    a = create(Article, :profile => c, :name => 'test-article', :author => profile, :published => true)

    xhr :get, :view_page, :profile => c.identifier, :page => a.path, :toolbar => true

    assert_tag :tag => 'a', :content => 'New article'
  end

  should 'display message if user was removed' do
    article = profile.articles.create(:name => 'comment test')
    to_be_removed = create_user('removed_user').person
    comment = article.comments.create(:author => to_be_removed, :title => 'Test Comment', :body => 'My author does not exist =(')
    to_be_removed.destroy

    get :view_page, :profile => profile.identifier, :page => article.path

    assert_tag :tag => 'span', :content => '(removed user)', :attributes => {:class => 'comment-user-status icon-user-removed'}
  end

  should 'show only first paragraph of blog posts if visualization_format is short' do
    login_as(profile.identifier)

    blog = Blog.create!(:name => 'A blog test', :profile => profile, :visualization_format => 'short')

    blog.posts << TinyMceArticle.create!(:name => 'first post', :parent => blog, :profile => profile, :body => '<p>Content to be displayed.</p> Anything')

    get :view_page, :profile => profile.identifier, :page => blog.path

    assert_tag :tag => 'div', :attributes => { :class => 'short-post'}, :content => /Content to be displayed./
    assert_no_tag :tag => 'div', :attributes => { :class => 'short-post'}, :content => /Anything/
  end

  should 'show only first paragraph with picture of posts if visualization_format is short+pic' do
    login_as(profile.identifier)

    blog = Blog.create!(:name => 'A blog test', :profile => profile, :visualization_format => 'short+pic')

    blog.posts << TinyMceArticle.create!(:name => 'first post', :parent => blog, :profile => profile, :body => '<p>Content to be displayed.</p> <img src="pic.jpg">')

    get :view_page, :profile => profile.identifier, :page => blog.path

    assert_select '.blog-post .post-pic' do |el|
      assert_match /background-image:url\(pic.jpg\)/, el.to_s
    end
  end

  should 'display link to edit blog for allowed' do
    blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
    login_as(profile.identifier)
    xhr :get, :view_page, :profile => profile.identifier, :page => blog.path, :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/edit/#{blog.id}" }, :content => 'Configure blog' }
  end

  # Forum

  should 'list unpublished forum posts to owner with a different class' do
    login_as('testinguser')
    forum = Forum.create!(:name => 'A forum test', :profile => profile)
    forum.posts << TextileArticle.create!(:name => 'Post', :profile => profile, :parent => forum, :published => false)

    get :view_page, :profile => profile.identifier, :page => [forum.path]
    assert_tag :tag => 'tr', :attributes => {:class => /not-published/}
  end

  should 'not list unpublished forum posts to a not logged person' do
    forum = Forum.create!(:name => 'A forum test', :profile => profile)
    forum.posts << TextileArticle.create!(:name => 'Post', :profile => profile, :parent => forum, :published => false)

    get :view_page, :profile => profile.identifier, :page => [forum.path]
    assert_no_tag :tag => 'a', :content => "Post"
  end

  should 'display pagination links of forum' do
    forum = Forum.create!(:name => 'A forum test', :profile => profile, :posts_per_page => 5)
    for n in 1..10
      forum.posts << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => forum)
    end
    assert_equal 10, forum.posts.size

    get :view_page, :profile => profile.identifier, :page => [forum.path]
    assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{forum.path}?npage=2", :rel => 'next' }
  end

  should 'display first page of forum posts' do
    forum = Forum.create!(:name => 'My forum', :profile => profile, :posts_per_page => 5)
    for n in 1..10
      art = TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => forum)
      art.updated_at = (10 - n).days.ago
      art.stubs(:record_timestamps).returns(false)
      art.save!
    end
    assert_equal 10, forum.posts.size

    get :view_page, :profile => profile.identifier, :page => [forum.path]
    for n in 1..5
      assert_no_tag :tag => 'a', :content => "Post #{n}", :parent => { :tag => 'td', :parent => { :tag => 'tr', :attributes => { :class => /forum-post/ } } }
    end
    for n in 6..10
      assert_tag :tag => 'a', :content => "Post #{n}", :parent => { :tag => 'td', :parent => { :tag => 'tr', :attributes => { :class => /forum-post/ } } }
    end
  end

  should 'display others pages of forum posts' do
    forum = Forum.create!(:name => 'My forum', :profile => profile, :posts_per_page => 5)
    now = Time.now
    for n in 1..10
      Time.stubs(:now).returns(now - 10.days + n.days)
      forum.children << art = TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => forum)
    end
    assert_equal 10, forum.posts.size

    get :view_page, :profile => profile.identifier, :page => [forum.path], :npage => 2
    for n in 1..5
      assert_tag :tag => 'a', :content => "Post #{n}", :parent => { :tag => 'td', :parent => { :tag => 'tr', :attributes => { :class => /forum-post/ } } }
    end
    for n in 6..10
      assert_no_tag :tag => 'a', :content => "Post #{n}", :parent => { :tag => 'td', :parent => { :tag => 'tr', :attributes => { :class => /forum-post/ } } }
    end
  end

  should 'set year and month filter from URL params for forum' do
    forum = Forum.create!(:name => "forum", :profile => profile)
    profile.articles << forum

    past_post = create(TextileArticle, :name => "past post", :profile => profile, :parent => forum, :published_at => forum.created_at - 1.year)
    current_post = TextileArticle.create!(:name => "current post", :profile => profile, :parent => forum)
    forum.children << past_post
    forum.children << current_post

    year, month = forum.created_at.year.to_s, '%02d' % forum.created_at.month

    get :view_page, :profile => profile.identifier, :page => [profile.forum.path], :year => year, :month => month

    assert_no_tag :tag => 'a', :content => past_post.title
    assert_tag :tag => 'a', :content => current_post.title
  end

  should "display 'New discussion topic' when create children of forum" do
    login_as(profile.identifier)
    a = Forum.create!(:name => 'article folder', :profile => profile)
    Article.stubs(:short_description).returns('bli')
    xhr :get, :view_page, :profile => profile.identifier, :page => [a.path], :toolbar => true
    assert_tag :tag => 'a', :content => 'New discussion topic'
  end

  should "display same label for new article button of forum parent" do
    login_as(profile.identifier)
    a = Forum.create!(:name => 'article folder', :profile => profile)
    Article.stubs(:short_description).returns('bli')
    t = TextileArticle.create!(:name => 'first post', :parent => a, :profile => profile)
    xhr :get, :view_page, :profile => profile.identifier, :page => [t.path], :toolbar => true
    assert_tag :tag => 'a', :content => 'New discussion topic'
  end

  should 'display icon-edit button to author topic' do
    community = fast_create(Community)
    admin = fast_create(Person)
    community.add_member(admin)
    author = create_user('author').person
    community.add_member(author)

    forum = Forum.create(:profile => community, :name => 'Forum test', :body => 'Forum test')
    post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :author_id => author.id)

    login_as(author.identifier)
    get :view_page, :profile => community.identifier, :page => post.path.split('/')

    assert_select "div#article-actions a.icon-edit"
  end

  should 'display icon-delete button to author topic' do
    community = fast_create(Community)
    admin = fast_create(Person)
    community.add_member(admin)
    author = create_user('author').person
    community.add_member(author)

    forum = Forum.create(:profile => community, :name => 'Forum test', :body => 'Forum test')
    post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :author_id => author.id)

    login_as(author.identifier)
    get :view_page, :profile => community.identifier, :page => post.path.split('/')

    assert_select "div#article-actions a.icon-delete"
  end

  should 'add meta tag to rss feed on view forum' do
    login_as(profile.identifier)
    profile.articles << Forum.new(:name => 'Forum', :profile => profile)
    get :view_page, :profile => profile.identifier, :page => ['forum']
    assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Forum', :href => "http://#{environment.default_hostname}/testinguser/forum/feed" }
  end

  should 'add meta tag to rss feed on view post forum' do
    login_as(profile.identifier)
    profile.articles << Forum.new(:name => 'Forum', :profile => profile)
    profile.forum.posts << TextileArticle.new(:name => 'first post', :parent => profile.forum, :profile => profile)
    get :view_page, :profile => profile.identifier, :page => ['forum', 'first-post']
    assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Forum', :href => "http://#{environment.default_hostname}/testinguser/forum/feed" }
  end

  should "not display 'Upload files' when viewing forum" do
    login_as(profile.identifier)
    b = Forum.create!(:name => 'article folder', :profile => profile)
    xhr :get, :view_page, :profile => profile.identifier, :page => b.path, :toolbar => true
    assert_no_tag :tag => 'a', :content => 'Upload files', :attributes => {:href => /parent_id=#{b.id}/}
  end

  should "not display 'Upload files' when viewing post from a forum" do
    login_as(profile.identifier)
    b = Forum.create!(:name => 'article folder', :profile => profile)
    forum_post = TextileArticle.create!(:name => 'children-article', :profile => profile, :parent => b)
    xhr :get, :view_page, :profile => profile.identifier, :page => forum_post.path, :toolbar => true
    assert_no_tag :tag => 'a', :content => 'Upload files', :attributes => {:href => /parent_id=#{b.id}/}
  end

  should 'display link to edit forum for allowed' do
    forum = fast_create(Forum, :profile_id => profile.id, :path => 'forum')
    login_as(profile.identifier)
    xhr :get, :view_page, :profile => profile.identifier, :page => forum.path, :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/edit/#{forum.id}" }, :content => 'Configure forum' }
  end

  should 'display add translation link if article is translatable' do
    environment.languages = ['en']
    environment.save
    login_as @profile.identifier
    textile = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'textile', :language => 'en')
    xhr :get, :view_page, :profile => @profile.identifier, :page => textile.path, :toolbar => true
    assert_tag :a, :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?article%5Btranslation_of_id%5D=#{textile.id}&type=#{TextileArticle}" }
  end

  should 'not display add translation link if article is not translatable' do
    login_as @profile.identifier
    blog = fast_create(Blog, :profile_id => @profile.id, :path => 'blog')
    xhr :get, :view_page, :profile => @profile.identifier, :page => blog.path, :toolbar => true
    assert_no_tag :a, :attributes => { :content => 'Add translation', :class => /icon-locale/ }
  end

  should 'not display add translation link if article hasnt a language defined' do
    login_as @profile.identifier
    textile = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'textile')
    xhr :get, :view_page, :profile => @profile.identifier, :page => textile.path, :toolbar => true
    assert_no_tag :a, :attributes => { :content => 'Add translation', :class => /icon-locale/ }
  end

  should 'display translations link if article has translations' do
    login_as @profile.identifier
    textile     = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'textile', :language => 'en')
    translation = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'translation', :language => 'es', :translation_of_id => textile)
    xhr :get, :view_page, :profile => @profile.identifier, :page => textile.path, :toolbar => true
    assert_tag :a, :attributes => { :class => /article-translations-menu/, :onmouseover => /toggleSubmenu/ }
  end

  should 'not be redirected if already in translation' do
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en')
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article)
    @request.env['HTTP_REFERER'] = "http://localhost:3000/#{@profile.identifier}/#{es_article.path}"
    FastGettext.stubs(:locale).returns('es')
    get :view_page, :profile => @profile.identifier, :page => es_article.path
    assert_response :success
    assert_equal es_article, assigns(:page)
  end

  should 'not be redirected if article does not have a language' do
    FastGettext.stubs(:locale).returns('es')
    article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'article')
    get :view_page, :profile => @profile.identifier, :page => article.path
    assert_response :success
    assert_equal article, assigns(:page)
  end

  should 'not be redirected if http_referer is a translation' do
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en')
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article)
    @request.env['HTTP_REFERER'] = "http://localhost:3000/#{@profile.identifier}/#{es_article.path}"
    FastGettext.stubs(:locale).returns('es')
    get :view_page, :profile => @profile.identifier, :page => en_article.path
    assert_response :success
    assert_equal en_article, assigns(:page)
  end

  should 'not be redirected to transition if came from edit' do
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en')
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article)
    FastGettext.stubs(:locale).returns('es')
    @request.env['HTTP_REFERER'] = "http://localhost/myprofile/#{@profile.identifier}/cms/edit/#{en_article.id}"
    get :view_page, :profile => @profile.identifier, :page => es_article.path
    assert_response :success
    assert_equal es_article, assigns(:page)
  end

  should 'not be redirected to transition if came from new' do
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en')
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article)
    FastGettext.stubs(:locale).returns('es')
    @request.env['HTTP_REFERER'] = "http://localhost/myprofile/#{@profile.identifier}/cms/new"
    get :view_page, :profile => @profile.identifier, :page => es_article.path
    assert_response :success
    assert_equal es_article, assigns(:page)
  end

  should 'replace article for his translation at blog listing if blog option is enabled' do
    FastGettext.stubs(:locale).returns('es')
    blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
    blog.display_posts_in_current_language = true
    blog.save
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article)

    get :view_page, :profile => @profile.identifier, :page => blog.path
    assert_tag :div, :attributes => { :id => "post-#{es_article.id}" }
    assert_no_tag :div, :attributes => { :id => "post-#{en_article.id}" }
  end

  should 'not display article at blog listing if blog option is enabled and there is no translation for the language' do
    FastGettext.stubs(:locale).returns('pt')
    blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
    blog.display_posts_in_current_language = true
    blog.save
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article)
    pt_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'pt', :parent_id => blog.id, :translation_of_id => en_article)

    en_article2 = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
    es_article2 = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article2)


    get :view_page, :profile => @profile.identifier, :page => blog.path

    assert_equal [pt_article], assigns(:posts)
  end

  should 'list all posts at blog listing if blog option is disabled' do
    FastGettext.stubs(:locale).returns('es')
    blog = Blog.create!(:name => 'A blog test', :profile => profile, :display_posts_in_current_language => false)
    blog.posts << es_post = TextileArticle.create!(:name => 'Spanish Post', :profile => profile, :parent => blog, :language => 'es')
    blog.posts << en_post = TextileArticle.create!(:name => 'English Post', :profile => profile, :parent => blog, :language => 'en', :translation_of_id => es_post.id)
    get :view_page, :profile => profile.identifier, :page => [blog.path]
    assert_equal 2, assigns(:posts).size
    assert_tag :div, :attributes => { :id => "post-#{es_post.id}" }
    assert_tag :div, :attributes => { :id => "post-#{en_post.id}" }
  end

  should 'display only native translations at blog listing if blog option is enabled' do
    FastGettext.stubs(:locale).returns('es')
    blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
    blog.display_posts_in_current_language = true
    blog.save!
    en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
    es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article)
    blog.posts = [en_article, es_article]

    get :view_page, :profile => @profile.identifier, :page => blog.path
    assert_equal [es_article], assigns(:posts)
  end

  should 'display reply to comment button if authenticated' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    article.save!
    Comment.destroy_all
    comment = article.comments.create!(:author => profile, :title => 'a comment', :body => 'lalala')
    login_as 'testuser'
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_tag :tag => 'a', :attributes => { :class => /comment-actions-reply/ }
  end

  should 'display reply to comment button if not authenticated' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    article.save!
    comment = article.comments.build(:author => profile, :title => 'a comment', :body => 'lalala')
    comment.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_tag :tag => 'a', :attributes => { :class => /comment-actions-reply/ }
  end

  should 'display replies if comment has replies' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    article.save!
    comment1 = article.comments.build(:author => profile, :title => 'a comment', :body => 'lalala')
    comment1.save!
    comment2 = article.comments.build(:author => profile, :title => 'a comment', :body => 'replying to lalala', :reply_of_id => comment1.id)
    comment2.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_tag :tag => 'ul', :attributes => { :class => 'comment-replies' }
  end

  should 'not display replies if comment does not have replies' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    article.save!
    comment = article.comments.build(:author => profile, :title => 'a comment', :body => 'lalala')
    comment.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_no_tag :tag => 'ul', :attributes => { :class => 'comment-replies' }
  end

  should 'add an zero width space every 4 caracters of comment urls' do
    url = 'www.an.url.to.be.splited.com'
    a = @profile.articles.build(:name => 'test')
    a.save!
    c = a.comments.create!(:author => @profile, :title => 'An url', :body => url, :source => a)
    get :view_page, :profile => @profile.identifier, :page => [ 'test' ]
    assert_tag :a, :attributes => { :href => "http://" + url}, :content => url.scan(/.{4}/).join('&#x200B;')
  end

  should 'not show a post comment button on top if there is only one comment' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    Comment.destroy_all
    article.save!
    comment = article.comments.build(:author => profile, :title => 'hi', :body => 'hello')
    comment.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_no_tag :tag => 'a', :attributes => { :id => 'top-post-comment-button' }
  end

  should 'not show a post comment button on top if there are no comments' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    Comment.destroy_all
    article.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_no_tag :tag => 'a', :attributes => { :id => 'top-post-comment-button' }
  end

  should 'show a post comment button on top if there are at least two comments' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    Comment.destroy_all
    article.save!
    comment1 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello')
    comment1.save!
    comment2 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello')
    comment2.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_tag :tag => 'a', :attributes => { :id => 'top-post-comment-button' }
  end

  should 'not show a post comment button on top if there are one comment and one reply' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test')
    Comment.destroy_all
    article.save!
    comment1 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello')
    comment1.save!
    comment2 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello', :reply_of_id => comment1.id)
    comment2.save!
    get :view_page, :profile => 'testuser', :page => [ 'test' ]
    assert_no_tag :tag => 'a', :attributes => { :id => 'top-post-comment-button' }
  end

  should 'suggest article link displayed into article-actions div' do
    community = fast_create(Community)
    blog = fast_create(Blog, :profile_id => community.id, :path => 'blog')
    xhr :get, :view_page, :profile => community.identifier, :page => [ 'blog' ], :toolbar => true
    assert_tag :tag => 'a', :attributes => { :id => 'suggest-article-link' }
  end

  should 'render toolbar when it is an ajax request' do
    community = fast_create(Community)
    blog = fast_create(Blog, :profile_id => community.id, :path => 'blog')
    xhr :get, :view_page, :profile => community.identifier, :page => ['blog'], :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-header' }
  end

  should 'add class to body tag if is on profile homepage' do
    profile = fast_create(Profile)
    blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
    profile.home_page = blog
    profile.save
    get :view_page, :profile => profile.identifier, :page => ['blog']
    assert_tag :tag => 'body', :attributes => { :class => /profile-homepage/ }
  end

  should 'not add class to body tag if is not on profile homepage' do
    profile = fast_create(Profile)
    blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
    get :view_page, :profile => profile.identifier, :page => ['blog']
    assert_no_tag :tag => 'body', :attributes => { :class => /profile-homepage/ }
  end

  should 'not display article actions button if any plugins says so' do
    class Plugin1 < Noosfero::Plugin
      def content_remove_edit(content); true; end
    end
    class Plugin2 < Noosfero::Plugin
      def content_remove_edit(content); false; end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)

    login_as('testinguser')
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_no_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/cms/edit/#{profile.home_page.id}" } }
  end

  should 'expire article actions button if any plugins says so' do
    class Plugin1 < Noosfero::Plugin
      def content_expire_edit(content); 'This button is expired.'; end
      def content_expire_clone(content); 'This button is expired.'; end
    end
    class Plugin2 < Noosfero::Plugin
      def content_expire_edit(content); nil; end
      def content_expire_clone(content); nil; end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)

    login_as('testinguser')
    xhr :get, :view_page, :profile => 'testinguser', :page => [], :toolbar => true
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => 'This button is expired.', :class => 'button with-text icon-edit disabled' } }
  end

  should 'remove email from article followers when unfollow' do
    profile = create_user('testuser').person
    follower_email = 'john@doe.br'
    article = profile.articles.create(:name => 'test')
    article.followers = [follower_email]
    article.save

    assert_includes Article.find(article.id).followers, follower_email
    post :view_page, :profile => profile.identifier, :page => [article.name], :unfollow => 'commit', :email => follower_email
    assert_not_includes Article.find(article.id).followers, follower_email
  end

  should 'not display comments marked as spam' do
    article = fast_create(Article, :profile_id => profile.id)
    ham = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :title => 'some content')
    spam = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :spam => true, :title => 'this is a spam')

    get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
    assert_no_tag :tag => 'h4', :content => /spam/
  end

  should 'add extra content on comment form from plugins' do
    class Plugin1 < Noosfero::Plugin
      def comment_form_extra_contents(args)
        proc {
          hidden_field_tag('comment[some_field_id]', 1)
        }
      end
    end
    class Plugin2 < Noosfero::Plugin
      def comment_form_extra_contents(args)
        proc {
          hidden_field_tag('comment[another_field_id]', 1)
        }
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    Environment.default.enable_plugin(Plugin1.name)
    Environment.default.enable_plugin(Plugin2.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]

    assert_tag :tag => 'input', :attributes => {:name => 'comment[some_field_id]', :type => 'hidden'}
    assert_tag :tag => 'input', :attributes => {:name => 'comment[another_field_id]', :type => 'hidden'}
  end

  should 'filter comments with scope defined by the plugins' do
    class Plugin1 < Noosfero::Plugin
      def unavailable_comments(scope)
        scope.where(:user_agent => 'Jack')
      end
    end

    class Plugin2 < Noosfero::Plugin
      def unavailable_comments(scope)
        scope.where(:referrer => 'kernel.org')
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    Environment.default.enable_plugin(Plugin1)
    Environment.default.enable_plugin(Plugin2)
    profile = fast_create(Profile)
    article = fast_create(Article, :profile_id => profile.id)
    c1 = fast_create(Comment, :source_id => article.id, :user_agent => 'Jack', :referrer => 'kernel.org')
    c2 = fast_create(Comment, :source_id => article.id, :user_agent => 'Rose', :referrer => 'kernel.org')
    c3 = fast_create(Comment, :source_id => article.id, :user_agent => 'Jack', :referrer => 'google.com')

    get :view_page, :profile => profile.identifier, :page => [ article.path ]

    assert_includes assigns(:comments), c1
    assert_not_includes assigns(:comments), c2
    assert_not_includes assigns(:comments), c3
  end

  should 'display pagination links of comments' do
    article = fast_create(Article, :profile_id => profile.id)
    for n in 1..15
      article.comments.create!(:author => profile, :title => "some title #{n}", :body => 'some body #{n}')
    end
    assert_equal 15, article.comments.count

    get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
    assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_page=2", :rel => 'next' }
  end

  should 'not escape acceptable HTML in list of blog posts' do
    login_as('testinguser')
    blog = Blog.create!(:name => 'A blog test', :profile => profile)
    blog.posts << TinyMceArticle.create!(
      :name => 'Post',
      :profile => profile,
      :parent => blog,
      :published => true,
      :body => "<p>This is a <strong>bold</strong> statement right there!</p>"
    )

    get :view_page, :profile => profile.identifier, :page => [blog.path]
    assert_tag :tag => 'strong', :content => /bold/
  end

  should 'add extra content on article header from plugins' do
    class Plugin1 < Noosfero::Plugin
      def article_header_extra_contents(args)
        proc {
          content_tag('div', '', :class => 'plugin1')
        }
      end
    end
    class Plugin2 < Noosfero::Plugin
      def article_header_extra_contents(args)
        proc {
          content_tag('div', '', :class => 'plugin2')
        }
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    Environment.default.enable_plugin(Plugin1.name)
    Environment.default.enable_plugin(Plugin2.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    xhr :get, :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], :toolbar => true

    assert_tag :tag => 'div', :attributes => {:class => 'plugin1'}
    assert_tag :tag => 'div', :attributes => {:class => 'plugin2'}
  end

  should 'display link to download of non-recognized file types on its page' do
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'bin/unknown'), :profile => profile)
    get :view_page, file.url
    assert_match /#{file.public_filename}/, @response.body
  end

  should 'not count hit from bots' do
    article = fast_create(Article, :profile_id => profile.id)
    assert_no_difference 'article.hits' do
      @request.env['HTTP_USER_AGENT'] = 'bot'
      get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
      @request.env['HTTP_USER_AGENT'] = 'spider'
      get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
      @request.env['HTTP_USER_AGENT'] = 'crawler'
      get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
      @request.env['HTTP_USER_AGENT'] = '(http://some-crawler.com)'
      get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
      article.reload
    end
  end

  should 'manage  private article visualization' do
    community = Community.create(:name => 'test-community')
    community.add_member(@profile)
    community.save!

    blog = community.articles.find_by_name("Blog")

    article = TinyMceArticle.create(:name => 'Article to be shared with images',
                                    :body => 'This article should be shared with all social networks',
                                    :profile => community,
                                    :published => false,
                                    :show_to_followers => true)
    article.parent = blog
    article.save!

    otheruser = create_user('otheruser').person
    community.add_member(otheruser)
    login_as(otheruser.identifier)

    get :view_page, :profile => community.identifier, "page" => 'blog'

    assert_response :success
    assert_tag :tag => 'h1', :attributes => { :class => /title/ }, :content => article.name

    article.show_to_followers = false
    article.save!

    get :view_page, :profile => community.identifier, "page" => 'blog'

    assert_no_tag :tag => 'h1', :attributes => { :class => /title/ }, :content => article.name
  end

  should 'add extra toolbar actions on article from plugins' do
    class Plugin1 < Noosfero::Plugin
      def article_extra_toolbar_buttons(article)
        {:title => 'some_title1', :icon => 'some_icon1', :url => {}}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name])

    Environment.default.enable_plugin(Plugin1.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => "some_title1" }}
  end

  should 'add more than one extra toolbar actions on article from plugins' do
    class Plugin1 < Noosfero::Plugin
      def article_extra_toolbar_buttons(article)
        {:title => 'some_title1', :icon => 'some_icon1', :url => {}}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def article_extra_toolbar_buttons(article)
        {:title => 'some_title2', :icon => 'some_icon2', :url => {}}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    Environment.default.enable_plugin(Plugin1.name)
    Environment.default.enable_plugin(Plugin2.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => "some_title1" }}
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => "some_title2" }}
  end

  should 'add icon attribute in extra toolbar actions on article from plugins' do
    class Plugin1 < Noosfero::Plugin
      def article_extra_toolbar_buttons(article)
        {:title => 'some_title', :icon => 'some_icon', :url => {}}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name])

    Environment.default.enable_plugin(Plugin1.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon/ }}
  end

  should 'add url attribute in extra toolbar actions on article from plugins' do
    class Plugin1 < Noosfero::Plugin
      def article_extra_toolbar_buttons(article)
        {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name])

    Environment.default.enable_plugin(Plugin1.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/someurl" }}
  end

  should 'use context method in extra toolbar actions on article from plugins' do
    class Plugin1 < Noosfero::Plugin
      def article_extra_toolbar_buttons(article)
        if profile.public?
          {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'}
        else
          {:title => 'another_title', :icon => 'another_icon', :url => '/anotherurl'}
        end
       end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name])

    Environment.default.enable_plugin(Plugin1.name)

    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')

    profile.public_profile = false
    profile.save
    login_as(profile.identifier)
    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/anotherurl" }}
  end

  should  'show lead,image and title in compact blog visualization' do
    community = Community.create(:name => 'test-community')
    community.add_member(@profile)
    community.save!

    blog = community.articles.find_by_name("Blog")
    blog.visualization_format = 'compact'
    blog.save!

    article = TinyMceArticle.create(:name => 'Article to be shared with images',
                                    :body => 'This article should be shared with all social networks',
                                    :profile => @profile,
                                    :published => false,
                                    :abstract => "teste teste teste",
                                    :show_to_followers => true,
                                    :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} )
    article.parent = blog
    article.save!

    login_as(@profile.identifier)


    get :view_page, :profile => community.identifier, "page" => 'blog'

    assert_tag :tag => 'div', :attributes => { :class => 'article-compact-image' }
    assert_tag :tag => 'div', :attributes => { :class => 'article-compact-abstract-with-image' }
  end

  should 'not count a visit twice for the same user' do
    profile = create_user('someone').person
    login_as(@profile.identifier)
    page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text')
    page.save!

    get :view_page, :profile => profile.identifier, :page => 'myarticle'
    page.reload
    assert_equal 1, page.hits

    get :view_page, :profile => profile.identifier, :page => 'myarticle'
    page.reload
    assert_equal 1, page.hits
  end

  should 'not count a visit twice for unlogged users' do
     profile = create_user('someone').person
     page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text')
     page.save!

     get :view_page, :profile => profile.identifier, :page => 'myarticle'
     page.reload
     assert_equal 1, page.hits

     get :view_page, :profile => profile.identifier, :page => 'myarticle'
     page.reload
     assert_equal 1, page.hits
  end

  should 'show blog image only inside blog cover' do
    blog = create(Blog, :profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
    blog.save!
    get :view_page, :profile => profile.identifier, :page => [blog.path]

    assert_select '.blog-cover > img', 1
    assert_select '.article-body-img > img', 0
  end

end