ref: master
test/unit/article_test.rb
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 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 |
# encoding: UTF-8 require_relative "../test_helper" class ArticleTest < ActiveSupport::TestCase fixtures :environments def setup ActiveSupport::TestCase::setup user = User.current = create_user 'testing' @profile = user.person end attr_reader :profile should 'have and require an associated profile' do a = Article.new a.valid? assert a.errors[:profile_id.to_s].present? a.profile = profile a.valid? refute a.errors[:profile_id.to_s].present? end should 'require value for name' do a = Article.new a.valid? assert a.errors[:name.to_s].present? a.name = 'my article' a.valid? refute a.errors[:name.to_s].present? end should 'limit length of names' do a = build(Article, :name => 'a'*151) a.valid? assert a.errors[:name.to_s].present? a.name = 'a'*150 a.valid? refute a.errors[:name.to_s].present? end should 'require value for slug and path if name is filled' do a = build(Article, :name => 'test article') a.slug = nil a.path = nil a.valid? assert a.errors[:slug.to_s].present? assert a.errors[:path.to_s].present? end should 'not require value for slug and path if name is blank' do a = Article.new a.valid? refute a.errors[:slug.to_s].present? refute a.errors[:path.to_s].present? end should 'act as versioned' do a = create(Article, :name => 'my article', :body => 'my text', :profile_id => profile.id) assert_equal 1, a.versions(true).size a.name = 'some other name' a.save! assert_equal 2, a.versions(true).size end should 'act as taggable' do a = create(Article, :name => 'my article', :profile_id => profile.id) a.tag_list = ['one', 'two'] tags = a.tag_list assert tags.include?('one') assert tags.include?('two') end should 'act as filesystem' do a = create(Article, :profile_id => profile.id) b = create(Article, :profile_id => profile.id, :parent_id => a.id) b.save! assert_equal "#{a.slug}/#{b.slug}", b.path a = Article.find(a.id); a.name = 'another name' a.save! b.reload assert_equal "another-name/#{b.slug}", b.path end should 'provide HTML version' do profile = create_user('testinguser').person a = fast_create(Article, :name => 'my article', :profile_id => profile.id) a.expects(:body).returns('the body of the article') assert_equal 'the body of the article', a.to_html end should 'provide HTML version when body is nil' do a = fast_create(Article, :profile_id => profile.id, :body => nil) assert_equal '', a.to_html end should 'provide short html version' do a = fast_create(Article, :body => 'full body', :abstract => 'lead', :profile_id => profile.id) expects(:display_short_format).with(a).once instance_eval(&a.to_html(:format=>'short')) end should 'provide full html version' do a = fast_create(Article, :body => 'full body', :abstract => 'lead') assert_equal 'full body', a.to_html(:format=>'full body') end should 'provide automatic abstract (excerpt) of HTML version' do env = Environment.default env.automatic_abstract_length = 55 env.save profile = create_user('testinguser').person a = fast_create(Article, :name => 'my article', :profile_id => profile.id, :body => " <p>The first paragraph of the article.</p> <p>The second paragraph</p> <p>The third which <a href=\"link\">is a really biiiiiiig paragraph</a> jds ksajdhf ksdfkjhsdh fakdshf askdjhfsd lfhsdlkfa dslkfah dskjahsd faksdhfk sdfkas fkjshfk sdhjf sdkjf sdkj fkdsjhfal ksdjhflaksjdhdsghfg <br /><img src='http://this_is_an_url/this_is_an_image.png' style='this_is_a_style'>sjhfgsdjhf sdjhgf asdjf sadj fadjhs gfas dkjgf asdjhf asdjh fjkdsg fjsdgf asdjf sadjlgf jsçlkdsjhfdsa lksajsalj aldja lkja slkdjal aj dasldkjas lkjsdj kj sjdlkjsdkfjsl lkjsdkf lk jsdkfjsjflsj kjdlsfjdslfj</p> ", :abstract => '') assert_match /The first paragraph of the article\. The second paragraph The third which is a really biiiiiiig paragraph jds ksajdhf ksdfkjhsdh fakdshf askdjhfsd lfhsdlkfa dslkfah dskjahsd faksdhfk sdfkas fkjshfk sdhjf sdkjf sdkj fkdsjhfal ksdjhflaksjdhdsghfg sjhfgsdjhf sdjhgf asdjf sadj fadjhs gfas dkjgf asdjhf asdjh fjkdsg fjsdgf asdjf sadjlgf jsçlkdsjhfdsa lksajsalj aldja lkja slkdjal aj dasldkjas lkjsdj kj \.\.\./, a.to_html(:format=>'short') assert_equal a.to_html(:format=>'short')[0..112], "<div class=\"short-post\"><img src='http://this_is_an_url/this_is_an_image.png' class = 'automatic-abstract-thumb'>" end should 'inform the icon to be used' do assert_equal 'text-html', Article.icon_name end should 'provide a (translatable) description' do result = 'the description' a = Article.new a.expects(:_).returns(result) assert_same result, a.mime_type_description end should 'not accept articles with same slug under the same level' do # top level articles first profile = create_user('testinguser').person a1 = profile.articles.build(:name => 'test') a1.save! # cannot add another top level article with same slug a2 = profile.articles.build(:name => 'test') a2.valid? assert a2.errors[:slug.to_s].present? # now create a child of a1 a3 = profile.articles.build(:name => 'test') a3.parent = a1 a3.valid? refute a3.errors[:slug.to_s].present? a3.save! # cannot add another child of a1 with same slug a4 = profile.articles.build(:name => 'test') a4.parent = a1 a4.valid? assert a4.errors[:slug.to_s].present? end should 'last_changed_by be a person' do a = profile.articles.build(:name => 'test') # must be a person assert_raise ActiveRecord::AssociationTypeMismatch do a.last_changed_by = Profile.new end assert_nothing_raised do a.last_changed_by = Person.new a.save! end end should 'created_by be a person' do a = profile.articles.build(:name => 'test') # must be a person assert_raise ActiveRecord::AssociationTypeMismatch do a.created_by = Profile.new end assert_nothing_raised do a.created_by = Person.new a.save! end end should 'not show private documents as recent' do p = create_user('usr1').person Article.destroy_all first = fast_create(TextArticle, :profile_id => p.id, :name => 'first', :published => true) second = fast_create(TextArticle, :profile_id => p.id, :name => 'second', :published => false) assert_equal [ first ], Article.recent(nil) end should 'not show unpublished documents as recent' do p = create_user('usr1').person Article.destroy_all first = fast_create(TextArticle, :profile_id => p.id, :name => 'first', :published => true) second = fast_create(TextArticle, :profile_id => p.id, :name => 'second', :published => false) assert_equal [ first ], Article.recent(nil) end should 'not show documents from a private profile as recent' do p = fast_create(Person, :public_profile => false) Article.destroy_all first = fast_create(TextArticle, :profile_id => p.id, :name => 'first', :published => true) second = fast_create(TextArticle, :profile_id => p.id, :name => 'second', :published => false) assert_equal [ ], Article.recent(nil) end should 'not show documents from a invisible profile as recent' do p = fast_create(Person, :visible => false) Article.destroy_all first = fast_create(TextArticle, :profile_id => p.id, :name => 'first', :published => true) second = fast_create(TextArticle, :profile_id => p.id, :name => 'second', :published => false) assert_equal [ ], Article.recent(nil) end should 'order recent articles by published_at' do p = create_user('usr1').person Article.destroy_all now = Time.now.in_time_zone first = create(Article, :name => 'first', :published => true, :created_at => now, :published_at => now, :profile_id => p.id) second = create(Article, :name => 'second', :published => true, :updated_at => now, :published_at => now + 1.second, :profile_id => p.id) assert_equal [ second, first ], Article.recent(2) Article.record_timestamps = false first.published_at = second.published_at + 1.second first.save! Article.record_timestamps = true assert_equal [ first, second ], Article.recent(2) end should 'not show UploadedFile as recent' do p = create_user('usr1').person Article.destroy_all first = build(UploadedFile, :profile => p, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')); first.save! second = fast_create(TextArticle, :profile_id => p.id, :name => 'second') assert_equal [ second ], Article.recent(nil) end should 'not show RssFeed as recent' do p = create_user('usr1').person Article.destroy_all first = fast_create(RssFeed, :profile_id => p.id, :name => 'my feed', :advertise => true) first.limit = 10; first.save! second = p.articles.build(:name => 'second'); second.save! assert_equal [ second ], Article.recent(nil) end should 'not show blog as recent' do p = create_user('usr1').person Article.destroy_all first = fast_create(Blog, :profile_id => p.id, :name => 'my blog', :advertise => true) second = create(Article, :name => 'second', :profile_id => p.id) assert_equal [ second ], Article.recent(nil) end should 'accept extra conditions to find recent' do p = create_user('usr1').person Article.destroy_all a1 = create(Article, :name => 'first', :profile_id => p.id) a2 = create(Article, :name => 'second', :profile_id => p.id) assert_equal [ a1 ], Article.recent(nil, :name => 'first') end should 'require that subclasses define description' do assert_raise NotImplementedError do Article.description end end should 'require that subclasses define short description' do assert_raise NotImplementedError do Article.short_description end end should 'indicate wheter children articles are allowed or not' do assert_equal true, Article.new.allow_children? end should 'provide a url to itself' do article = create(Article, :name => 'myarticle', :profile_id => profile.id) assert_equal(profile.url.merge(:page => ['myarticle']), article.url) end should 'provide a url to itself having a parent topic' do parent = create(Article, :name => 'parent', :profile_id => profile.id) child = create(Article, :name => 'child', :parent => parent, :profile_id => profile.id) assert_equal(profile.url.merge(:page => [ 'parent', 'child']), child.url) end should 'associate with categories' do env = Environment.default parent_cat = create(Category, :name => "parent category", :environment_id => env.id) c1 = create(Category, :name => "test category 1", :parent_id => parent_cat.id, :environment_id => env.id) c2 = create(Category, :name => "test category 2", :environment_id => env.id) article = create(Article, :name => 'withcategories', :profile_id => profile.id) article.save! article.add_category c1 article.add_category c2 assert_equivalent [c1,c2], article.categories(true) assert_equivalent [c1, parent_cat, c2], article.categories_including_virtual(true) end should 'remove comments when removing article' do assert_no_difference 'Comment.count' do a = create(Article, :name => 'test article', :profile_id => profile.id) assert_difference 'Comment.count', 1 do comment = a.comments.build comment.author = profile comment.title = 'test comment' comment.body = 'you suck!' comment.save! end a.destroy end end should 'list most commented articles' do Article.delete_all a1 = create(TextArticle, :name => "art 1", :profile_id => profile.id) a2 = create(TextArticle, :name => "art 2", :profile_id => profile.id) a3 = create(TextArticle, :name => "art 3", :profile_id => profile.id) 2.times { create(Comment, :title => 'test', :body => 'asdsad', :author => profile, :source => a2).save! } 4.times { create(Comment, :title => 'test', :body => 'asdsad', :author => profile, :source => a3).save! } # should respect the order (more commented comes first) assert_equal [a3, a2, a1], profile.articles.most_commented(3) end should 'identify itself as a non-folder' do refute Article.new.folder?, 'should identify itself as non-folder' end should 'identify itself as a non-blog' do refute Article.new.blog?, 'should identify itself as non-blog' end should 'always display if public content' do person = create_user('testuser').person assert_equal true, person.home_page.display_to?(nil) end should 'display to owner' do # a person with private contents ... person = create_user('testuser').person person.public_content = false person.save! # ... can see his own articles a = create(Article, :name => 'test article', :profile_id => person.id) assert_equal true, a.display_to?(person) end should 'cache children count' do owner = create_user('testuser').person art = create(Article, :name => 'ytest', :profile_id => owner.id) # two children articles create(Article, :profile => owner, :name => 'c1', :parent_id => art.id) create(Article, :profile => owner, :name => 'c2', :parent_id => art.id) art.reload assert_equal 2, art.children_count assert_equal 2, art.children.size end should 'categorize in the entire category hierarchy' do c1 = create(Category, :environment => Environment.default, :name => 'c1') c2 = create(Category, :environment => Environment.default, :name => 'c2', :parent_id => c1.id) c3 = create(Category, :environment => Environment.default, :name => 'c3', :parent_id => c2.id) owner = create_user('testuser').person art = create(Article, :name => 'ytest', :profile_id => owner.id) art.add_category(c3) assert_equal [c3], art.categories(true) assert_equal [art], c2.articles(true) assert_includes c3.articles(true), art assert_includes c2.articles(true), art assert_includes c1.articles(true), art assert_includes art.categories_including_virtual(true), c2 assert_includes art.categories_including_virtual(true), c1 end should 'redefine the entire category set at once' do c1 = create(Category, :environment => Environment.default, :name => 'c1') c2 = create(Category, :environment => Environment.default, :name => 'c2', :parent_id => c1.id) c3 = create(Category, :environment => Environment.default, :name => 'c3', :parent_id => c2.id) c4 = create(Category, :environment => Environment.default, :name => 'c4', :parent_id => c1.id) owner = create_user('testuser').person art = create(Article, :name => 'ytest', :profile_id => owner.id) art.add_category(c4) art.category_ids = [c2,c3].map(&:id) assert_equivalent [c2, c3], art.categories(true) assert_includes art.categories_including_virtual(true), c1 refute art.categories_including_virtual(true).include?(c4) end should 'be able to create an article already with categories' do parent1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'parent1') c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1', :parent_id => parent1.id) c2 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c2') p = create_user('testinguser').person a = create(Article, :name => 'test', :category_ids => [c1.id, c2.id], :profile_id => p.id) assert_equivalent [c1, c2], a.categories(true) assert_includes a.categories_including_virtual(true), parent1 end should 'not add a category twice to article' do c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1') c2 = create(Category, :environment => Environment.default, :name => 'c2', :parent_id => c1.id) c3 = create(Category, :environment => Environment.default, :name => 'c3', :parent_id => c1.id) owner = create_user('testuser').person art = create(Article, :name => 'ytest', :profile_id => owner.id) art.category_ids = [c2,c3,c3].map(&:id) categories = art.categories(true) categories_including_virtual = art.categories_including_virtual(true) assert_not_includes categories, c1 assert_includes categories, c2 assert_includes categories, c3 assert_includes categories_including_virtual, c1 assert_includes categories_including_virtual, c2 assert_includes categories_including_virtual, c3 end should 'accept only visible categories' do cat = ProductCategory.new cat.visible_for_articles = false refute Article.new.accept_category?(cat) end should 'accept published attribute' do assert_respond_to Article.new, :published assert_respond_to Article.new, :published= end should 'say that logged off user cannot see private article' do article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) refute article.display_to?(nil) end should 'say that not member of profile cannot see private article' do article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) person = create_user('test_user').person refute article.display_to?(person) end should 'say that member user can not see private article' do article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false, :show_to_followers => false) person = create_user('test_user').person profile.affiliate(person, Profile::Roles.member(profile.environment.id)) refute article.display_to?(person) end should 'say that profile admin can see private article' do org = fast_create(Profile, :name => 'test profile', :identifier => 'test_profile') article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) org.affiliate(profile, Profile::Roles.admin(org.environment.id)) assert article.display_to?(profile) end should 'say that profile moderator can see private article' do org = fast_create(Profile, :name => 'test profile', :identifier => 'test_profile') article = fast_create(Article, :name => 'test article', :profile_id => org.id, :published => false) org.affiliate(profile, Profile::Roles.moderator(org.environment.id)) assert article.display_to?(profile) end should 'show article to non member if article public but profile private' do profile.update public_profile: false article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => true) person1 = create_user('test_user1').person profile.affiliate(person1, Profile::Roles.member(profile.environment.id)) person2 = create_user('test_user2').person assert article.display_to?(nil) assert article.display_to?(person2) assert article.display_to?(person1) end should 'make new article private if created inside a private folder' do folder = fast_create(Folder, :name => 'my_intranet', :profile_id => profile.id, :published => false) article = fast_create(Article, :name => 'my private article', :profile_id => profile.id, :parent_id => folder.id) refute article.published? end should 'save as private' do folder = fast_create(Folder, :name => 'my_intranet', :profile_id => profile.id, :published => false) article = fast_create(Article, :name => 'my private article') article.profile = profile article.parent = folder article.save! article.reload refute article.published? end should 'not allow friends of private person see the article' do person = create_user('test_user').person article = create(Article, :name => 'test article', :profile => person, :published => false, :show_to_followers => false) friend = create_user('test_friend').person person.add_friend(friend) person.save! friend.save! refute article.display_to?(friend) end should 'display private articles to people who can view private content' do person = create_user('test_user').person article = fast_create(Article, :name => 'test article', :profile_id => person.id, :published => false) admin_user = create_user('admin_user').person admin_user.stubs(:has_permission?).with('view_private_content', article.profile).returns('true') assert article.display_to?(admin_user) end should 'make a copy of the article as child of it' do person = create_user('test_user').person a = create(Article, :name => 'test article', :body => 'some text', :profile_id => person.id) b = a.copy(:parent => a, :profile => a.profile) assert_includes a.children, b assert_equal 'some text', b.body end should 'make a copy of the article to other profile' do p1 = create_user('test_user1').person p2 = create_user('test_user2').person a = create(Article, :name => 'test article', :body => 'some text', :profile_id => p1.id) b = a.copy(:parent => a, :profile => p2) p2 = Person.find(p2.id) assert_includes p2.articles, b assert_equal 'some text', b.body end should 'mantain the type in a copy' do p = create_user('test_user').person a = fast_create(Folder, :name => 'test folder', :profile_id => p.id) b = a.copy(:parent => a, :profile => p) assert_kind_of Folder, b end should 'not copy slug' do a = fast_create(Article, :slug => 'slug123') b = a.copy({}) assert a.slug != b.slug end should 'load article under an old path' do p = create_user('test_user').person a = create(Article, :name => 'old-name', :profile_id => p.id) old_path = a.explode_path a.name = 'new-name' a.save! page = Article.find_by_old_path old_path assert_equal a.path, page.path end should 'load new article name equal of another article old name' do p = create_user('test_user').person a1 = create(Article, :name => 'old-name', :profile_id => p.id) old_path = a1.explode_path a1.name = 'new-name' a1.save! a2 = p.articles.create!(:name => 'old-name') page = Article.find_by_old_path old_path assert_equal a2.path, page.path end should 'article with most recent version with the name must be loaded if no aritcle with the name' do p = create_user('test_user').person a1 = create(Article, :name => 'old-name', :profile_id => p.id) old_path = a1.explode_path a1.name = 'new-name' a1.save! a2 = create(Article, :name => 'old-name', :profile_id => p.id) a2.name = 'other-new-name' a2.save! page = Article.find_by_old_path old_path assert_equal a2.path, page.path end should 'not return an article of a different user' do p1 = create_user('test_user').person a = create(Article, :name => 'old-name', :profile_id => p1.id) old_path = a.explode_path a.name = 'new-name' a.save! p2 = create_user('another_user').person page = p2.articles.find_by_old_path old_path assert_nil page end should 'identify if belongs to blog' do p = create_user('user_blog_test').person blog = fast_create(Blog, :name => 'Blog test', :profile_id => p.id) post = fast_create(TextArticle, :name => 'First post', :profile_id => p.id, :parent_id => blog.id) assert post.belongs_to_blog? end should 'not belongs to blog' do p = create_user('user_blog_test').person folder = fast_create(Folder, :name => 'Not Blog', :profile_id => p.id) a = fast_create(TextArticle, :name => 'Not blog post', :profile_id => p.id, :parent_id => folder.id) refute a.belongs_to_blog? end should 'has comments notifier true by default' do a = Article.new assert a.notify_comments? end should 'has moderate comments false by default' do a = create(Article, :name => 'my article', :body => 'my text', :profile_id => profile.id) a.reload assert a.moderate_comments == false end should 'save a article with moderate comments as true' do a = create(Article, :name => 'my article', :body => 'my text', :profile_id => profile.id, :moderate_comments => true) a.reload assert a.moderate_comments end should 'moderate_comments? return true if moderate_comments variable is true' do a = Article.new a.moderate_comments= true assert a.moderate_comments? end should 'moderate_comments? return false if moderate_comments variable is false' do a = Article.new a.moderate_comments= false refute a.moderate_comments? end should 'hold hits count' do a = fast_create(Article, :name => 'Test article', :profile_id => profile.id) a.hits = 10 a.save! a.reload assert_equal 10, a.hits end should 'increment hit counter when hitted' do a = fast_create(Article, :name => 'Test article', :profile_id => profile.id, :hits => 10) a.hit assert_equal 11, a.hits a.reload assert_equal 11, a.hits end should 'have display_hits setting with default true' do a = fast_create(Article, :name => 'Test article', :profile_id => profile.id) assert_respond_to a, :display_hits assert_equal true, a.display_hits end should 'can display hits' do a = fast_create(Article, :name => 'Test article', :profile_id => profile.id) assert_respond_to a, :can_display_hits? assert_equal true, a.can_display_hits? end should 'return a view url when image' do image = create(UploadedFile, :profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) assert_equal image.url.merge(:view => true), image.view_url end should 'not return a view url when common article' do a = fast_create(Article, :name => 'Test article', :profile_id => profile.id) assert_equal a.url, a.view_url end should 'have published_at' do assert_respond_to Article.new, :published_at end should 'fill published_at with current date if not set' do now = Time.now.in_time_zone Time.stubs(:now).returns(now) a = create(Article, :name => 'Published at', :profile_id => profile.id) assert_equal now, a.published_at end should 'use npage to compose cache key' do a = fast_create(Article, :name => 'Published at', :profile_id => profile.id) assert_match(/-npage-2/,a.cache_key(:npage => 2)) end should 'use year and month to compose cache key' do a = fast_create(Article, :name => 'Published at', :profile_id => profile.id) assert_match(/-year-2009-month-04/, a.cache_key(:year => '2009', :month => '04')) end should 'use revision number to compose cache key' do a = fast_create(Article, :name => 'Versioned article', :profile_id => profile.id) assert_match(/-version-2/,a.cache_key(:version => 2)) end should 'use language in cache key' do a = Article.new assert_not_equal a.cache_key({}, nil, ''), a.cache_key({}, nil, 'pt') end should 'not be highlighted by default' do a = Article.new refute a.highlighted end should 'get tagged with tag' do a = create(Article, :name => 'Published at', :profile_id => profile.id, :tag_list => 'bli') as = Article.tagged_with('bli') assert_includes as, a end should 'get tagged with tag that contains special chars' do a = create(Article, :name => 'Published at', :profile_id => profile.id, :tag_list => 'Métodos Ágeis') as = Article.tagged_with('Métodos Ágeis') assert_includes as, a end should 'not get tagged with tag from other environment' do article_from_this_environment = create(Article, :profile => profile, :tag_list => 'bli') other_environment = fast_create(Environment) user_from_other_environment = create_user('other_user', :environment => other_environment).person article_from_other_enviroment = create(Article, :profile => user_from_other_environment, :tag_list => 'bli') tagged_articles_in_other_environment = other_environment.articles.tagged_with('bli') assert_includes tagged_articles_in_other_environment, article_from_other_enviroment assert_not_includes tagged_articles_in_other_environment, article_from_this_environment end should 'ignore category with zero as id' do a = create(Article, :name => 'a test article', :profile_id => profile.id) c = fast_create(Category, :name => 'test category', :environment_id => profile.environment.id, :parent_id => 0) a.category_ids = ['0', c.id, nil] assert a.save assert_equal [c], a.categories # also ignore parent with id = 0 assert_equal [c], a.categories_including_virtual a = profile.articles.find_by name: 'a test article' assert_equal [c], a.categories end should 'add owner on cache_key when has profile' do a = create(Article, :name => 'a test article', :profile_id => profile.id) assert_match(/-owner/, a.cache_key({}, profile)) end should 'not add owner on cache_key when has no profile' do a = create(Article, :name => 'a test article', :profile_id => profile.id) assert_no_match(/-owner/, a.cache_key({})) end should 'add owner on cache_key when profile is community' do c = fast_create(Community) a = create(Article, :name => 'a test article', :profile_id => c.id) assert_match(/-owner/, a.cache_key({}, c)) end should 'allow author to edit if is publisher' do c = fast_create(Community) p = create_user_with_permission('test_user', 'publish_content', c) a = create(Article, :name => 'a test article', :author => p, :profile_id => c.id) assert a.allow_post_content?(p) end should 'allow user with "Manage content" permissions to edit' do c = fast_create(Community) p = create_user_with_permission('test_user', 'post_content', c) a = create(Article, :name => 'a test article', :profile_id => c.id) assert a.allow_post_content?(p) end should 'update slug from name' do article = create(Article, :name => 'A test article', :profile_id => profile.id) assert_equal 'a-test-article', article.slug article.name = 'Changed name' assert_equal 'changed-name', article.slug end should 'find articles in a specific category' do env = Environment.default parent_category = create(Category, :name => "parent category", :environment_id => env.id) category_with_articles = create(Category, :name => "Category with articles", :parent_id => parent_category.id, :environment_id => env.id) category_without_articles = create(Category, :name => "Category without articles", :environment_id => env.id) article_in_category = create(Article, :name => 'Article in category', :profile_id => profile.id) article_in_category.add_category(category_with_articles) assert_includes profile.articles.in_category(category_with_articles), article_in_category assert_includes profile.articles.in_category(parent_category), article_in_category assert_not_includes profile.articles.in_category(category_without_articles), article_in_category end should 'has external_link attr' do assert_nothing_raised NoMethodError do build(Article, :external_link => 'http://some.external.link') end end should 'validates format of external_link' do article = build(Article, :external_link => 'http://invalid-url') article.valid? assert_not_nil article.errors[:external_link] end should 'put http in external_link' do article = build(Article, :external_link => 'url.without.http') assert_equal 'http://url.without.http', article.external_link end should 'list only published articles' do profile = fast_create(Person) published = create(Article, :name => 'Published', :published => true, :profile_id => profile.id) unpublished = create(Article, :name => 'Unpublished', :published => false, :profile_id => profile.id) assert_equal [ published ], profile.articles.published end should 'sanitize tags after save article' do article = fast_create(Article, :slug => 'article-with-tags', :profile_id => profile.id) tag = build(Tag, name: "TV Web w<script type='javascript'></script>") assert_match /[<>]/, tag.name article.tag_list.add(tag.name) article.save! assert_no_match /[<>]/, article.tags.last.name end should 'strip HTML from tag names after save article' do article = fast_create(Article, :slug => 'article-with-tags', :profile_id => profile.id) tag = build(Tag, name: "TV Web w<script type=...") assert_match /</, tag.name article.tag_list.add(tag.name) article.save! assert_no_match /</, article.tags.last.name end should 'sanitize name before validation' do article = Article.new article.name = "<h1 Bla </h1>" article.valid? assert_no_match /<[^>]*</, article.name end should 'not doubly escape quotes in the name' do person = fast_create(Person) community = fast_create(Community) community.add_member(profile) article = fast_create(Article, :name => 'article name', :profile_id => person.id) a = create(ApproveArticle, :article => article, :target => community, :requestor => profile) a.finish published = community.articles.find_by(name: 'article name') published.name = 'title with "quotes"' published.save assert_equal 'title with "quotes"', published.name end should 'remove script tags from name' do a = build(Article, :name => 'hello <script>alert(1)</script>') a.valid? assert_no_match(/<script>/, a.name) end should 'return truncated title in short_title' do article = Article.new article.name = 'a123456789abcdefghij' assert_equal 'a123456789ab...', article.short_title end should 'return abstract as lead' do a = build(Article, :abstract => 'lead') assert_equal 'lead', a.lead end should 'return automatic abstract as lead by default' do a = Article.new a.stubs(:automatic_abstract).returns('beginning') assert_equal 'beginning', a.lead end should 'return automatic abstract as lead with empty but non-null abstract' do a = Article.new(:abstract => '') a.stubs(:automatic_abstract).returns('beginning') assert_equal 'beginning', a.lead end should 'have short lead' do a = fast_create(TextArticle, :body => '<p>' + ('a' *180) + '</p>') assert_equal 170, a.short_lead.length end should 'remove html from short lead' do a = build(Article, :body => "<p>an article with html that should be <em>removed</em></p>") assert_equal 'an article with html that should be removed', a.short_lead end should 'track action when a published article is created outside a community' do article = create(TextArticle, :profile_id => profile.id) ta = article.activity assert_equal article.name, ta.get_name assert_equal article.url, ta.get_url end should 'track action when a published article is created in a community' do community = fast_create(Community) p1 = create_user.person p2 = create_user.person p3 = create_user.person community.add_member(p1) community.add_member(p2) User.current = p1.user article = create(TextArticle, :profile_id => community.id) activity = article.activity process_delayed_job_queue assert_equal 3, ActionTrackerNotification.where(action_tracker_id: activity.id).count assert_equivalent [p1,p2,community], ActionTrackerNotification.where(action_tracker_id: activity.id).map(&:profile) end should 'destroy activity when a published article is removed' do a = create(TextArticle, :profile_id => profile.id) assert_difference 'ActionTracker::Record.count', -1 do a.destroy end end should 'notifiable is false by default' do a = fast_create(Article) refute a.notifiable? end should 'not notify activity by default on create' do ActionTracker::Record.delete_all create Article, :name => 'test', :profile_id => profile.id, :published => true assert_equal 0, ActionTracker::Record.count end should 'not notify activity by default on update' do ActionTracker::Record.delete_all a = create Article, :name => 'bar', :profile_id => profile.id, :published => true a.name = 'foo' a.save! assert_equal 0, ActionTracker::Record.count end should 'not notify activity by default on destroy' do ActionTracker::Record.delete_all a = create Article, :name => 'bar', :profile_id => profile.id, :published => true a.destroy assert_equal 0, ActionTracker::Record.count end should 'create activity' do a = create TextArticle, :name => 'bar', :profile_id => profile.id, :published => true a.activity.destroy assert_nil a.activity a.create_activity assert_not_nil a.activity end should "the action_tracker_target method be defined" do assert Article.method_defined?(:action_tracker_target) end should "the action_tracker_target method return the article profile" do profile = fast_create(Person) article = fast_create(Article, :profile_id => profile.id) assert_equal profile, article.action_tracker_target profile = fast_create(Community) article = fast_create(Article, :profile_id => profile.id) assert_equal profile, article.action_tracker_target end should 'create the notification to organization and all organization members' do Profile.destroy_all ActionTracker::Record.destroy_all community = fast_create(Community) User.current = create_user member_1 = User.current.person community.add_member(member_1) article = create TextArticle, :name => 'Tracked Article 1', :profile_id => community.id first_activity = article.activity assert_equal [first_activity], ActionTracker::Record.where(verb: 'create_article') process_delayed_job_queue assert_equal 2, ActionTrackerNotification.where(action_tracker_id: first_activity.id).count member_2 = fast_create(Person) community.add_member(member_2) article2 = create TextArticle, :name => 'Tracked Article 2', :profile_id => community.id second_activity = article2.activity assert_equivalent [first_activity, second_activity], ActionTracker::Record.where(verb: 'create_article') process_delayed_job_queue assert_equal 3, ActionTrackerNotification.where(action_tracker_id: second_activity.id).count end should 'create notifications to followers when creating an article' do friend = fast_create(Person) circle = Circle.create!(:person=> friend, :name => "Zombies", :profile_type => 'Person') friend.follow(profile, circle) Article.destroy_all ActionTracker::Record.destroy_all ActionTrackerNotification.destroy_all User.current = profile.user article = create(TextArticle, :profile_id => profile.id) process_delayed_job_queue assert_equal friend, ActionTrackerNotification.last.profile end should 'create the notification to the follower when one follower has the notification and the other no' do f1 = fast_create(Person) circle = Circle.create!(:person=> f1, :name => "Zombies", :profile_type => 'Person') f1.follow(profile, circle) User.current = profile.user article = create TextArticle, :name => 'Tracked Article 1', :profile_id => profile.id assert_equal 1, ActionTracker::Record.where(verb: 'create_article').count process_delayed_job_queue assert_equal 2, ActionTrackerNotification.where(action_tracker_id: article.activity.id).count f2 = fast_create(Person) circle2 = Circle.create!(:person=> f2, :name => "Zombies", :profile_type => 'Person') f2.follow(profile, circle2) article2 = create TextArticle, :name => 'Tracked Article 2', :profile_id => profile.id assert_equal 2, ActionTracker::Record.where(verb: 'create_article').count process_delayed_job_queue assert_equal 3, ActionTrackerNotification.where(action_tracker_id: article2.activity.id).count end should 'destroy activity and notifications of followers when destroying an article' do friend = fast_create(Person) circle = Circle.create!(:person=> friend, :name => "Zombies", :profile_type => 'Person') friend.follow(profile, circle) Article.destroy_all ActionTracker::Record.destroy_all ActionTrackerNotification.destroy_all User.current = profile.user article = create(TextArticle, :profile_id => profile.id) activity = article.activity process_delayed_job_queue assert_equal 2, ActionTrackerNotification.where(action_tracker_id: activity.id).count assert_difference 'ActionTrackerNotification.count', -2 do article.destroy end assert_raise ActiveRecord::RecordNotFound do ActionTracker::Record.find(activity.id) end end should 'destroy action_tracker and notifications when an article is destroyed in a community' do community = fast_create(Community) p1 = create_user.person p2 = create_user.person community.add_member(p1) community.add_member(p2) User.current = p1.user article = create(TextArticle, :profile_id => community.id) activity = article.activity process_delayed_job_queue assert_equal 3, ActionTrackerNotification.where(action_tracker_id: activity.id).count assert_difference 'ActionTrackerNotification.count', -3 do article.destroy end assert_raise ActiveRecord::RecordNotFound do ActionTracker::Record.find(activity.id) end end should 'found articles with published date between a range' do start_date = DateTime.parse('2010-07-06') end_date = DateTime.parse('2010-08-02') article_found1 = fast_create(Article, :published_at => start_date) article_found2 = fast_create(Article, :published_at => end_date) article_not_found = fast_create(Article, :published_at => end_date + 1.month) assert_includes Article.by_range(start_date..end_date), article_found1 assert_includes Article.by_range(start_date..end_date), article_found2 assert_not_includes Article.by_range(start_date..end_date), article_not_found end should 'calculate first/end day of a month' do assert_equal 1, (DateTime.parse('2010-07-06')).at_beginning_of_month.day assert_equal 31, (DateTime.parse('2010-07-06')).at_end_of_month.day end should 'not be a forum by default' do refute fast_create(Article).forum? end should 'not have posts by default' do refute fast_create(Article).has_posts? end should 'get article galleries' do a = fast_create(Article, :profile_id => profile.id) g = fast_create(Gallery, :profile_id => profile.id) assert_equal [g], profile.articles.galleries end should 'has many translations' do a = build(Article) assert_raises(ActiveRecord::AssociationTypeMismatch) { a.translations << 1 } assert_nothing_raised { a.translations << build(Article) } end should 'belongs to translation of' do a = build(Article) assert_raises(ActiveRecord::AssociationTypeMismatch) { a.translation_of = 1 } assert_nothing_raised { a.translation_of = build(Article) } end should 'has language' do a = build(Article) assert_nothing_raised { a.language = 'en' } end should 'validate inclusion of language' do a = build(Article, :profile_id => profile.id) a.language = '12' a.valid? assert a.errors[:language.to_s].present? a.language = 'en' a.valid? refute a.errors[:language.to_s].present? end should 'language can be blank' do a = build(Article) a.valid? refute a.errors[:language.to_s].present? a.language = '' a.valid? refute a.errors[:language.to_s].present? end should 'article is not translatable' do a = build(Article) refute a.translatable? end should 'get native translation' do native_article = fast_create(Article) article_translation = fast_create(Article) native_article.translations << article_translation assert_equal native_article, native_article.native_translation assert_equal native_article, article_translation.native_translation end should 'list possible translations' do native_article = fast_create(Article, :language => 'pt', :profile_id => profile.id ) article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id) possible_translations = native_article.possible_translations refute possible_translations.include?('en') assert possible_translations.include?('pt') end should 'verify if translation is already in use' do native_article = fast_create(Article, :language => 'pt') article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id) a = build(Article, :profile => profile) a.language = 'en' a.translation_of = native_article a.valid? assert a.errors[:language.to_s].present? a.language = 'es' a.valid? refute a.errors[:language.to_s].present? end should 'verify if native translation is already in use' do native_article = fast_create(Article, :language => 'pt') a = build(Article, :profile => profile) a.language = 'pt' a.translation_of = native_article a.valid? assert a.errors[:language.to_s].present? a.language = 'es' a.valid? refute a.errors[:language.to_s].present? end should 'translation have a language' do native_article = fast_create(Article, :language => 'pt') a = build(Article, :profile_id => profile.id) a.translation_of = native_article a.valid? assert a.errors[:language.to_s].present? a.language = 'en' a.valid? refute a.errors[:language.to_s].present? end should 'native translation have a language' do native_article = fast_create(Article, :profile_id => profile.id ) a = build(Article, :profile_id => profile.id) a.language = 'en' a.translation_of = native_article a.valid? n = a.errors.count native_article.language = 'pt' native_article.save a.valid? assert_equal n - 1, a.errors.count end should 'rotate translations when root article is destroyed' do native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) fast_create(Article, :language => 'en', :translation_of_id => native_article.id, :profile_id => @profile.id) fast_create(Article, :language => 'es', :translation_of_id => native_article.id, :profile_id => @profile.id) new_root = native_article.translations.order(:created_at).first child = (native_article.translations - [new_root]).first native_article.destroy assert new_root.translation_of.nil? assert new_root.translations.include?(child) end should 'rotate one translation when root article is destroyed' do native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id, :profile_id => @profile.id) native_article.destroy assert translation.translation_of.nil? assert translation.translations.empty? end should 'get self if article does not a language' do article = fast_create(Article, :profile_id => @profile.id) assert_equal article, article.get_translation_to('en') end should 'get self if language article is blank' do article = fast_create(Article, :language => '', :profile_id => @profile.id) assert_equal article, article.get_translation_to('en') end should 'get self if article is the translation' do article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) assert_equal article, article.get_translation_to('pt') end should 'get the native translation if it is the translation' do native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id, :profile_id => @profile.id) assert_equal native_article, translation.get_translation_to('pt') end should 'get the translation if article has translation' do native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id, :profile_id => @profile.id) assert_equal translation, native_article.get_translation_to('en') end should 'get self if article does not has a translation' do native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) assert_nil native_article.get_translation_to('en') end should 'get only non translated articles' do profile.articles.delete_all native = fast_create(Article, :language => 'pt', :profile_id => profile.id) translation = fast_create(Article, :language => 'en', :translation_of_id => native.id, :profile_id => profile.id) assert_equal [native], profile.articles.native_translations end should 'not list own language as a possible translation if language has changed' do a = build(Article, :language => 'pt', :profile_id => profile.id) refute a.possible_translations.include?('pt') a = fast_create(Article, :language => 'pt', :profile_id => profile.id ) a.language = 'en' refute a.possible_translations.include?('en') end should 'list own language as a possible translation if language has not changed' do a = fast_create(Article, :language => 'pt', :profile_id => profile.id) assert a.possible_translations.include?('pt') end should 'have the author_name method defined' do assert Article.method_defined?('author_name') end should "the author_name returns the name of the article's author" do author = fast_create(Person) a = create(Article, :name => 'a test article', :author => author, :profile_id => profile.id) assert_equal author.name, a.author_name author.destroy a = Article.find(a.id) a.author_name = 'some name' assert_equal 'some name', a.author_name end should 'retrieve latest info from topic when has no comments' do forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) post = fast_create(TextArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now.in_time_zone, :author_id => profile.id) assert_equal post.updated_at, post.info_from_last_update[:date] assert_equal profile.name, post.info_from_last_update[:author_name] assert_equal profile.url, post.info_from_last_update[:author_url] end should 'retrieve latest info from comment when has comments' do forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) post = fast_create(TextArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now.in_time_zone) post.comments << build(Comment, :name => 'Guest', :email => 'guest@example.com', :title => 'test comment', :body => 'hello!') assert_equal post.comments.last.created_at, post.info_from_last_update[:date] assert_equal "Guest", post.info_from_last_update[:author_name] assert_nil post.info_from_last_update[:author_url] end should 'return only folders' do not_folders = [RssFeed, TextArticle, Event, TextArticle] folders = [Folder, Blog, Gallery, Forum] not_folders.each do |klass| item = fast_create(klass) assert_not_includes Article.folders(profile), item end folders.each do |klass| item = fast_create(klass) assert_includes Article.folders(profile), item end end should 'return no folders' do not_folders = [RssFeed, TextArticle, Event, TextArticle] folders = [Folder, Blog, Gallery, Forum] not_folders.each do |klass| item = fast_create(klass) assert_includes Article.no_folders(profile), item end folders.each do |klass| item = fast_create(klass) assert_not_includes Article.no_folders(profile), item end end should 'accept uploads if parent accept uploads' do folder = fast_create(Folder) child = fast_create(UploadedFile, :parent_id => folder.id) assert folder.accept_uploads? assert child.accept_uploads? end should 'not accept uploads if has no parent' do child = fast_create(UploadedFile) refute child.accept_uploads? end should 'not accept uploads if parent is a blog' do folder = fast_create(Blog) child = fast_create(UploadedFile, :parent_id => folder.id) refute child.accept_uploads? end should 'not accept uploads if parent is a forum' do folder = fast_create(Forum) child = fast_create(UploadedFile, :parent_id => folder.id) refute child.accept_uploads? end should 'get images paths in article body' do Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') a = build TextArticle, :profile => @profile a.body = 'Noosfero <img src="http://noosfero.com/test.png" /> test <img src="http://test.com/noosfero.png" />' assert_includes a.body_images_paths, 'http://noosfero.com/test.png' assert_includes a.body_images_paths, 'http://test.com/noosfero.png' end should 'always put article image first in images paths list in article body' do Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') a = create(TextArticle, :name => 'test', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }, :profile_id => @profile.id) a.save! a.body = 'Noosfero <img src="http://noosfero.com/test.png" /> test <img src="http://test.com/noosfero.png" />' a.image.stubs(:public_filename).returns('/files/rails.png') assert_equal 'http://noosfero.org/files/rails.png', a.body_images_paths[0] end should 'escape utf8 characters correctly' do Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') a = build TextArticle, profile: @profile a.body = 'Noosfero <img src="http://noosfero.com/cabeça.png" /> ' assert_includes a.body_images_paths, 'http://noosfero.com/cabe%C3%A7a.png' # check if after save (that is, after xss_terminate run) a.save! assert_includes a.body_images_paths, 'http://noosfero.com/cabe%C3%A7a.png' end should 'get absolute images paths in article body' do Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') a = build TextArticle, :profile => @profile a.body = 'Noosfero <img src="test.png" alt="Absolute" /> test <img src="/relative/path.png" />' assert_includes a.body_images_paths, 'http://noosfero.org/test.png' assert_includes a.body_images_paths, 'http://noosfero.org/relative/path.png' end should 'return empty if there are no images in article body' do Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') a = build Event, :profile => @profile a.body = 'Noosfero test' assert_equal [], a.body_images_paths end should 'return empty if body is nil' do Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') a = build Article, :profile => @profile assert_equal [], a.body_images_paths end should 'survive to a invalid src attribute while looking for images in body' do domain = Environment.default.domains.first || build(Domain, :name => 'localhost') article = build(Article, :body => "An article with invalid src in img tag <img src='path with spaces.png' />", :profile => @profile) assert_nothing_raised URI::InvalidURIError do assert_equal ["http://#{profile.environment.default_hostname}/path%20with%20spaces.png"], article.body_images_paths end end should 'find more recent contents' do Article.delete_all c1 = fast_create(TextArticle, :name => 'Testing article 1', :body => 'Article body 1', :profile_id => profile.id, :created_at => DateTime.now - 4) c2 = fast_create(TextArticle, :name => 'Testing article 2', :body => 'Article body 2', :profile_id => profile.id, :created_at => DateTime.now - 1) c3 = fast_create(TextArticle, :name => 'Testing article 3', :body => 'Article body 3', :profile_id => profile.id, :created_at => DateTime.now - 3) assert_equal [c2,c3,c1] , Article.more_recent c4 = fast_create(TextArticle, :name => 'Testing article 4', :body => 'Article body 4', :profile_id => profile.id, :created_at => DateTime.now - 2) assert_equal [c2,c4,c3,c1] , Article.more_recent end should 'respond to more comments' do assert_respond_to Article, :more_comments end should 'respond to more popular' do assert_respond_to Article, :more_popular end should "return the more recent label" do a = Article.new assert_equal "Created at: ", a.more_recent_label end should "return no comments if profile has 0 comments" do a = Article.new assert_equal 0, a.comments_count assert_equal "no comments", a.more_comments_label end should "return 1 comment on label if the content has 1 comment" do a = build(Article, :comments_count => 1) assert_equal 1, a.comments_count assert_equal "one comment", a.more_comments_label end should "return number of comments on label if the content has more than one comment" do a = build(Article, :comments_count => 4) assert_equal 4, a.comments_count assert_equal "4 comments", a.more_comments_label end should "return no views if profile has 0 views" do a = Article.new assert_equal 0, a.hits assert_equal "no views", a.more_popular_label end should "return 1 view on label if the content has 1 view" do a = build(Article, :hits => 1) assert_equal 1, a.hits assert_equal "one view", a.more_popular_label end should "return number of views on label if the content has more than one view" do a = build(Article, :hits => 4) assert_equal 4, a.hits assert_equal "4 views", a.more_popular_label end should 'delegate region info to profile' do Person.any_instance.expects(:region) Person.any_instance.expects(:region_id) article = fast_create(Article, :profile_id => profile.id) article.region article.region_id end should 'delegate environment info to profile' do Person.any_instance.expects(:environment) Person.any_instance.expects(:environment_id) article = fast_create(Article, :profile_id => profile.id) article.environment article.environment_id end should 'remove all categorizations when destroyed' do art = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id) cat = create(Category, :name => 'category 1', :environment_id => Environment.default.id) art.add_category cat art.destroy assert cat.articles.reload.empty? end should 'show more popular articles' do Article.destroy_all art1 = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id) art2 = create(Article, :name => 'article 2', :profile_id => fast_create(Person).id) art3 = create(Article, :name => 'article 3', :profile_id => fast_create(Person).id) art1.hits = 56; art1.save! art3.hits = 92; art3.save! art2.hits = 3; art2.save! assert_equal [art3, art1, art2], Article.more_popular end should 'show if article is public' do art1 = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id) art2 = create(Article, :name => 'article 2', :profile_id => fast_create(Person).id, :advertise => false) art3 = create(Article, :name => 'article 3', :profile_id => fast_create(Person).id, :published => false) art4 = create(Article, :name => 'article 4', :profile_id => fast_create(Person, :visible => false).id) art5 = create(Article, :name => 'article 5', :profile_id => fast_create(Person, :public_profile => false).id) articles = Article.is_public assert_includes articles, art1 assert_not_includes articles, art2 assert_not_includes articles, art3 assert_not_includes articles, art4 assert_not_includes articles, art5 end should 'not allow all community members to edit by default' do community = fast_create(Community) admin = fast_create(Person) member = fast_create(Person) community.add_admin(admin) community.reload community.add_member(member) a = build(Article, :profile => community) assert_equal false, a.allow_members_to_edit assert_equal false, a.allow_edit?(member) end should 'be able to allow all members of a community to edit' do community = fast_create(Community) admin = create_user('community-admin').person member = create_user.person community.add_admin(admin) community.add_member(member) a = build(Article, :profile => community) a.allow_members_to_edit = true assert_equal true, a.allow_edit?(member) end should 'not crash on allow_edit without a current user' do a = build(Article) a.allow_members_to_edit = true refute a.allow_edit?(nil) end should 'allow author to edit topic' do community = fast_create(Community) admin = fast_create(Person) community.add_member(admin) author = fast_create(Person) community.add_member(author) forum = Forum.create(:profile => community, :name => 'Forum test', :body => 'Forum test') post = fast_create(TextArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :author_id => author.id) assert post.allow_edit?(author) end should 'get first image from lead' do a = fast_create(Article, :body => '<p>Foo</p><p><img src="bar.png" />Bar<img src="foo.png" /></p>', :abstract => '<p>Lead</p><p><img src="leadbar.png" />Bar<img src="leadfoo.png" /></p>') assert_equal 'leadbar.png', a.first_image end should 'get first image from body' do a = fast_create(Article, :body => '<p>Foo</p><p><img src="bar.png" />Bar<img src="foo.png" /></p>') assert_equal 'bar.png', a.first_image end should 'get first image from having_image' do a = fast_create(Article, :body => '<p>Foo</p><p><img src="bar.png" /></p>', :abstract => '<p>Lead</p><p><img src="lead.png" /></p>' ) img = {} img.expects(:present?).returns true img.expects(:public_filename).returns 'pic.jpg' a.expects(:image).at_least_once.returns img assert_equal 'pic.jpg', a.first_image end should 'not get first image from anywhere' do a = fast_create(Article, :body => '<p>Foo</p><p>Bar</p>') assert_equal '', a.first_image end should 'store first image in tracked action' do a = create TextArticle, :name => 'Tracked Article', :body => '<p>Foo<img src="foo.png" />Bar</p>', :profile_id => profile.id assert_equal 'foo.png', a.first_image assert_equal 'foo.png', ActionTracker::Record.last.get_first_image end should 'be able to have a license' do license = create(License, :name => 'GPLv3', :environment => Environment.default) article = build(Article, :license_id => license.id) assert_equal license, article.license end should 'return license from a specific version' do cc = License.create!(:name => 'CC (by)', :environment => Environment.default) gpl = License.create!(:name => 'GPLv3', :environment => Environment.default) article = create(Article, :name => 'first version', :profile => profile, :license => cc) article.license = gpl article.save assert_equal cc, article.version_license(1) assert_equal gpl, article.version_license(2) end should 'update path if parent is changed' do f1 = create(Folder, :name => 'Folder 1', :profile => profile) f2 = create(Folder, :name => 'Folder 2', :profile => profile) article = create(TextArticle, :name => 'Sample Article', :parent_id => f1.id, :profile => profile) assert_equal [f1.path,article.slug].join('/'), article.path article.parent = f2 article.save! assert_equal [f2.path,article.slug].join('/'), article.path article.parent = nil article.save! assert_equal article.slug, article.path article.parent = f2 article.save! assert_equal [f2.path,article.slug].join('/'), article.path end should 'not allow parent as itself' do article = create(Article, :name => 'Sample Article', :profile => profile) article.parent = article article.valid? assert article.errors[:parent_id.to_s].present? end should 'not allow cyclical paternity' do a1 = create(Article, :name => 'Sample Article 1', :profile => profile) a2 = create(Article, :name => 'Sample Article 2', :profile => profile, :parent => a1) a3 = create(Article, :name => 'Sample Article 3', :profile => profile, :parent => a2) a1.parent = a3 a1.valid? assert a1.errors[:parent_id.to_s].present? end should 'set author_name before creating article if there is an author' do author = fast_create(Person) article = create(Article, :name => 'Test', :profile => profile, :author => author) assert_equal author.name, article.author_name author_name = author.name author.destroy article = Article.find(article.id) assert_equal author_name, article.author_name end should "author_id return the author id of the article's author" do author = fast_create(Person) article = create(Article, :name => 'Test', :profile => profile, :author => author) assert_equal author.id, article.author_id end should "author_id return nil if there is no article's author" do article = create(Article, :name => 'Test', :profile => profile, :author => nil) assert_nil article.author_id end should "return the author_name of a specific version" do author1 = fast_create(Person) author2 = fast_create(Person) article = create(Article, :name => 'first version', :profile => profile, :author => author1, :last_changed_by => author1) article.name = 'second version' article.last_changed_by = author2 article.save assert_equal author1.name, article.author_name(1) assert_equal author2.name, article.author_name(2) end should 'identify if belongs to forum' do p = create_user('user_forum_test').person forum = fast_create(Forum, :name => 'Forum test', :profile_id => p.id) post = fast_create(TextArticle, :name => 'First post', :profile_id => p.id, :parent_id => forum.id) assert post.belongs_to_forum? end should 'not belongs to forum' do p = create_user('user_forum_test').person blog = fast_create(Blog, :name => 'Not Forum', :profile_id => p.id) a = fast_create(TextArticle, :name => 'Not forum post', :profile_id => p.id, :parent_id => blog.id) refute a.belongs_to_forum? end should 'not belongs to forum if do not have a parent' do p = create_user('user_forum_test').person a = fast_create(TextArticle, :name => 'Orphan post', :profile_id => p.id) refute a.belongs_to_forum? end should 'save image on create article' do assert_difference 'Article.count' do p = create(Article, :name => 'test', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }, :profile_id => @profile.id) assert_equal p.image(true).filename, 'rails.png' end end should 'return articles with specific types' do Article.delete_all c1 = fast_create(TextArticle, :name => 'Testing article 1', :body => 'Article body 2', :profile_id => profile.id) c3 = fast_create(Event, :name => 'Testing article 3', :body => 'Article body 3', :profile_id => profile.id) c4 = fast_create(RssFeed, :name => 'Testing article 4', :body => 'Article body 4', :profile_id => profile.id) c5 = fast_create(TextArticle, :name => 'Testing article 5', :body => 'Article body 5', :profile_id => profile.id) assert_equivalent [c1,c5], Article.with_types(['TextArticle']) assert_equivalent [c3], Article.with_types(['Event']) end should 'not create version when receive a comment' do a = Article.new(:name => 'my article', :body => 'my text') a.profile = profile a.save! Comment.create!(:title => 'test', :body => 'asdsad', :author => profile, :source => a) assert_equal 1, a.versions.count end should 'get specific version' do article = Article.create!(:name => 'first version', :profile => profile) article.name = 'second version' article.save! article.name = 'third version' article.save! assert_equal 'first version', article.get_version(1).name assert_equal 'second version', article.get_version(2).name assert_equal 'third version', article.get_version(3).name end should 'get author by version' do p1 = fast_create(Person) p2 = fast_create(Person) p3 = fast_create(Person) article = create(Article, :name => 'first version', :profile => profile, :author => p1, :last_changed_by => p1) article.name = 'second version' article.last_changed_by = p2 article.save! article.last_changed_by = p3 article.name = 'third version' article.save! assert_equal p1, article.author_by_version(1) assert_equal p2, article.author_by_version(2) assert_equal p3, article.author_by_version(3) end should 'display_filter display only public articles if there is no user' do p = fast_create(Person) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [a], Article.display_filter(nil, p) end should 'display_filter display public articles for users' do user = create_user('someuser').person p = fast_create(Person) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter display private article last changed by user' do user = create_user('someuser').person p = fast_create(Person) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => false, :last_changed_by_id => user.id, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter display user private article of his own profile' do user = create_user('someuser').person user.stubs(:has_permission?).with(:view_private_content, user).returns(false) p = fast_create(Person) Article.delete_all a = fast_create(Article, :published => false, :profile_id => user.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, user) end should 'display_filter show profile private content if the user has view_private_content permission' do user = create_user('someuser').person p = fast_create(Person) Article.delete_all user.stubs(:has_permission?).with(:view_private_content, p).returns(false) a = fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, p) user.stubs(:has_permission?).with(:view_private_content, p).returns(true) assert_equal [a], Article.display_filter(user, p) end should 'display_filter show person private content to friends' do user = create_user('someuser').person p = fast_create(Person) p.add_friend(user) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter show person private content to friends when no profile is passed as parameter' do user = create_user('someuser').person p = fast_create(Person) user.add_friend(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, nil) end should 'display_filter show community private content to members' do user = create_user('someuser').person p = fast_create(Community) p.add_member(user) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter do not show person private content to non friends' do user = create_user('someuser').person p = fast_create(Person) refute p.is_a_friend?(user) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, p) end should 'display_filter do not show community private content to non members' do user = create_user('someuser').person p = fast_create(Community) refute user.is_member_of?(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, p) end should 'display_filter show community public content even it has no followers defined' do user = create_user('someuser').person p = fast_create(Community) refute user.is_member_of?(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter show person public content even it has no followers defined' do user = create_user('someuser').person p = fast_create(Community) refute user.is_a_friend?(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :show_to_followers => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter do not show person private content to non friends passing nil as profile parameter' do user = create_user('someuser').person p = fast_create(Person) refute p.is_a_friend?(user) refute user.is_admin? Article.delete_all fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, nil) end should 'display_filter show person public content to non friends passing nil as profile parameter' do user = create_user('someuser').person p = fast_create(Person) refute p.is_a_friend?(user) refute user.is_admin? Article.delete_all a1 = fast_create(Article, :profile_id => p.id) a2 = fast_create(Article) assert_equivalent [a1,a2], Article.display_filter(user, nil) end should 'display_filter do not show community private content to non members passing nil as profile parameter' do user = create_user('someuser').person p = fast_create(Community) refute user.is_member_of?(p) Article.delete_all fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, nil) end should 'display_filter show community public content to non members passing nil as profile parameter' do user = create_user('someuser').person p = fast_create(Community) refute user.is_member_of?(p) Article.delete_all a1 = fast_create(Article, :profile_id => p.id) a2 = fast_create(Article) assert_equivalent [a1,a2], Article.display_filter(user, nil) end should 'display_filter show community public content of private community for user members' do user = create_user('someuser').person p = fast_create(Community, :public_profile => false) p.add_member(user) assert user.is_member_of?(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter not show public content of private community for non members' do user = create_user('someuser').person p = fast_create(Community, :public_profile => false) refute user.is_member_of?(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, p) end should 'display_filter not show public content of private community for non members when user is nil' do p = fast_create(Community, :public_profile => false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(nil, p) end should 'display_filter show public content for non members when profile is nil' do user = create_user('someuser').person p = fast_create(Community, :public_profile => true) Article.delete_all a1 = fast_create(Article, :published => true, :profile_id => user.id) a2 = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equivalent [a1,a2], Article.display_filter(nil, user) end should 'display_filter show person public content of private person profile for user friends' do user = create_user('someuser').person p = fast_create(Person, :public_profile => false) p.add_friend(user) assert p.is_a_friend?(user) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) fast_create(Article, :published => false, :show_to_followers => false, :profile_id => p.id) assert_equal [a], Article.display_filter(user, p) end should 'display_filter not show public content of private person for non friends' do user = create_user('someuser').person p = fast_create(Person, :public_profile => false) refute user.is_a_friend?(p) user.stubs(:has_permission?).with(:view_private_content, p).returns(false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(user, p) end should 'display_filter not show public content of private person for non friends when user is nil' do p = fast_create(Person, :public_profile => false) Article.delete_all a = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equal [], Article.display_filter(nil, p) end should 'display_filter show public content for non friends when profile is nil' do user = create_user('someuser').person p = fast_create(Person, :public_profile => true) Article.delete_all a1 = fast_create(Article, :published => true, :profile_id => user.id) a2 = fast_create(Article, :published => true, :profile_id => p.id) fast_create(Article, :published => false, :profile_id => p.id) assert_equivalent [a1,a2], Article.display_filter(nil, user) end should 'update hit attribute of article array' do a1 = fast_create(Article) a2 = fast_create(Article) a3 = fast_create(Article) Article.hit([a1, a2, a3]) Article.hit([a2, a3]) assert_equal [1, 2, 2], [a1.hits, a2.hits, a3.hits] assert_equal [1, 2, 2], [a1.reload.hits, a2.reload.hits, a3.reload.hits] end should 'not update hit attribute of archiveds articles' do a1 = fast_create(Article) a2 = fast_create(Article, :archived => true) a3 = fast_create(Article, :archived => true) Article.hit([a1, a2, a3]) assert_equal [1, 0, 0], [a1.hits, a2.hits, a3.hits] assert_equal [1, 0, 0], [a1.reload.hits, a2.reload.hits, a3.reload.hits] end should 'vote in a article' do article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) profile.vote(article, 5) assert_equal 1, article.voters_who_voted.length assert_equal 5, article.votes_total end should 'be able to remove a voted article' do article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) profile.vote(article, 5) article.destroy end should 'be able to vote in an article without a user' do article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) assert_difference 'article.votes_for', 2 do Vote.create!(:voteable => article, :vote => 1) Vote.create!(:voteable => article, :vote => 1) end end should 'have can_display_media_panel with default false' do a = Article.new assert !a.can_display_media_panel? end should 'display media panel when allowed by the environment' do a = Article.new a.expects(:can_display_media_panel?).returns(true) environment = mock a.expects(:environment).returns(environment) environment.expects(:enabled?).with('media_panel').returns(true) assert a.display_media_panel? end should 'not display media panel when not allowed by the environment' do a = Article.new a.expects(:can_display_media_panel?).returns(true) environment = mock a.expects(:environment).returns(environment) environment.expects(:enabled?).with('media_panel').returns(false) assert !a.display_media_panel? end should 'have display_preview' do a = Article.new(:display_preview => false) assert !a.display_preview? end should 'return full_path' do p1 = fast_create(Profile) p2 = fast_create(Profile) p2.domains << Domain.create!(:name => 'p2.domain') a1 = fast_create(Article, :profile_id => p1.id) a2 = fast_create(Article, :profile_id => p2.id) assert_equal "/#{p1.identifier}/#{a1.path}", a1.full_path assert_equal "/#{a2.path}", a2.full_path end should "increment followers count when a person follow an article" do a = fast_create(Article) p = fast_create(Person) assert_difference "a.reload.followers_count" do a.person_followers << p end end should "decrement followers count when a person unfollow an article" do p = fast_create(Person) a = fast_create(Article, :profile_id => p) a.person_followers << p assert_difference "a.reload.followers_count", -1 do a.person_followers.destroy_all end end should 'the owner not in followers list' do person1 = create_user('article_owner').person person2 = create_user('article_follower').person article = fast_create(Article, :profile_id => person1.id) article.person_followers=[person2] article.save article.reload article.person_followers=[person2] article.save assert_equal [person2], article.reload.person_followers end should 'has a empty list of followers by default' do a = Article.new assert_equal [], a.person_followers end should 'a follower not be duplicated' do follower = create_user('article_follower').person article = fast_create(Article, :profile_id => fast_create(Person)) article.person_followers<< follower assert_raises (ActiveRecord::RecordNotUnique) { article.person_followers<< follower } end should 'an article be follower by many users' do article = fast_create(Article, :profile_id => fast_create(Person)) 1.upto(10).map do |n| article.person_followers<< fast_create(Person) end article.save assert_equal 10, article.reload.person_followers.count end should 'check if a article is archived' do folder = Folder.create!(:name => 'Parent Archived', :profile => profile) a1 = Article.create!(:name => 'Test', :profile => profile, :parent_id => folder.id, :archived => false) a2 = Article.create!(:name => 'Test 2', :profile => profile, :archived => true) folder.update_attributes(:archived => true) a1.reload assert a1.archived? assert a2.archived? end should 'try add a child article to a archived folder' do folder = Folder.create!(:name => 'Parent Archived', :profile => profile, :archived => true) err = assert_raises ActiveRecord::RecordInvalid do a1 = Article.create!(:name => 'Test', :profile => profile, :parent_id => folder.id, :archived => false) end assert_match 'Parent folder is archived', err.message end should 'have can_display_blocks with default true' do assert Article.can_display_blocks? end should 'is_editor true if the article editor is the same as te editor parameter' do article = Article.new(:editor => Article::Editor::TEXTILE) assert article.editor?(Article::Editor::TEXTILE) article = Article.new(:editor => Article::Editor::TINY_MCE) assert article.editor?(Article::Editor::TINY_MCE) article = Article.new(:editor => Article::Editor::RAW_HTML) assert article.editor?(Article::Editor::RAW_HTML) end should 'is_editor false if the article editor is not the same as te editor parameter' do article = Article.new(:editor => Article::Editor::TEXTILE) assert !article.editor?(Article::Editor::TINY_MCE) article = Article.new(:editor => Article::Editor::TINY_MCE) assert !article.editor?(Article::Editor::TEXTILE) article = Article.new(:editor => Article::Editor::RAW_HTML) assert !article.editor?(Article::Editor::TINY_MCE) end RESERVED_SLUGS = %w[ about activities ] RESERVED_SLUGS.map do |reserved| define_method "test_should_not_create_an_article_with_#{reserved}_title" do article = fast_create(Article) article.name = reserved article.valid? assert !article.errors[:title].empty? end end end |